brainleg.app.util.AppUtil.java Source code

Java tutorial

Introduction

Here is the source code for brainleg.app.util.AppUtil.java

Source

/*
 * Copyright 2011 Roman Stepanenko
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package brainleg.app.util;

import brainleg.app.exception.MaskRuleSyntaxProblemException;
import com.google.common.base.Splitter;
import com.google.common.collect.Lists;
import org.apache.commons.lang.SerializationException;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.util.Collections;
import java.util.List;

/**
 *
 * @author Roman Stepanenko
 */
public class AppUtil {
    /**
     * Generates a string to be sent to web site which contains only the user-specified
     * replacement dummy value and not the original trace text.
     *
     * @param replacementValue
     * @return
     */
    public static String generateMaskedTrace(String originalTrace, String replacementValue) {
        int atIndex = originalTrace.indexOf(ParseUtil.AT_SP);
        String masked = "BRAINLEG-MASKED:" + replacementValue + AppUtil.LS;
        if (atIndex < 0) {
            return masked;
        } else {
            return originalTrace.substring(0, atIndex + ParseUtil.AT_SP.length()) + masked;
        }
    }

    /**
     * <p>Deserializes an <code>Object</code> from the specified stream.</p>
     * <p/>
     * <p>The stream will be closed once the object is written. This
     * avoids the need for a finally clause, and maybe also exception
     * handling, in the application code.</p>
     * <p/>
     * <p>The stream passed in is not buffered internally within this method.
     * This is the responsibility of your application if desired.</p>
     *
     * @param inputStream the serialized object input stream, must not be null
     * @return the deserialized object
     * @throws IllegalArgumentException if <code>inputStream</code> is <code>null</code>
     * @throws SerializationException   (runtime) if the serialization fails
     */
    public static Object deserialize(InputStream inputStream) {
        if (inputStream == null) {
            throw new IllegalArgumentException("The InputStream must not be null");
        }
        ObjectInputStream in = null;
        try {
            // stream closed in the finally
            in = new ObjectInputStream(inputStream);
            return in.readObject();

        } catch (ClassNotFoundException ex) {
            throw new SerializationException(ex);
        } catch (IOException ex) {
            throw new SerializationException(ex);
        } finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (IOException ex) {
                // ignore close exception
            }
        }
    }

    /**
     * <p>Deserializes a single <code>Object</code> from an array of bytes.</p>
     *
     * @param objectData the serialized object, must not be null
     * @return the deserialized object
     * @throws IllegalArgumentException if <code>objectData</code> is <code>null</code>
     * @throws SerializationException   (runtime) if the serialization fails
     */
    public static Object deserialize(byte[] objectData) {
        if (objectData == null) {
            throw new IllegalArgumentException("The byte[] must not be null");
        }
        ByteArrayInputStream bais = new ByteArrayInputStream(objectData);
        return deserialize(bais);
    }

    public static final String LS = System.getProperty("line.separator");

    public static String getPluginVersion() {
        return "2.2";
    }
}