Example usage for org.apache.commons.lang SerializationUtils serialize

List of usage examples for org.apache.commons.lang SerializationUtils serialize

Introduction

In this page you can find the example usage for org.apache.commons.lang SerializationUtils serialize.

Prototype

public static byte[] serialize(Serializable obj) 

Source Link

Document

Serializes an Object to a byte array for storage/serialization.

Usage

From source file:fr.inria.oak.paxquery.translation.Logical2Pact.java

private static final Operator<Record>[] translate(Selection sel) {
    Operator<Record>[] childPlan = translate(sel.getChild());

    // create MapOperator for selecting some records
    MapOperator selection = MapOperator.builder(SelectionOperator.class).input(childPlan).name("Select")
            .build();//from   ww  w . jav  a2 s.c o m

    // selection configuration
    final String encodedNRSMD = DatatypeConverter
            .printBase64Binary(SerializationUtils.serialize(sel.getNRSMD()));
    selection.setParameter(PACTOperatorsConfiguration.NRSMD1_BINARY.toString(), encodedNRSMD);
    final String encodedPredicate = DatatypeConverter
            .printBase64Binary(SerializationUtils.serialize(sel.getPred()));
    selection.setParameter(PACTOperatorsConfiguration.PRED_BINARY.toString(), encodedPredicate);

    return new Operator[] { selection };
}

From source file:com.splicemachine.mrio.api.hive.SMSerDe.java

/**
 * This method takes an object representing a row of data from Hive, and
 * uses the ObjectInspector to get the data for each column and serialize
 * it./*w  ww .  jav  a2s.  com*/
 */
//@Override
public Writable serialize(Object obj, ObjectInspector oi) throws SerDeException {
    ExecRow row = null;
    int[] execRowFormatIds = null;
    try {
        List<NameType> nameTypes = sqlUtil.getTableStructure(tableName);
        execRowFormatIds = sqlUtil.getExecRowFormatIds(colNames, nameTypes);
        row = sqlUtil.getExecRow(execRowFormatIds);
        if (row == null)
            throw new SerDeException("ExecRow Cannot be Null");
    } catch (SQLException | StandardException | IOException e1) {
        throw new SerDeException(e1);
    }
    if (Log.isTraceEnabled())
        SpliceLogUtils.trace(Log, "serialize with obj=%s, oi=%s", obj, oi);
    if (oi.getCategory() != ObjectInspector.Category.STRUCT) {
        throw new SerDeException(
                getClass().toString() + " can only serialize struct types, but we got: " + oi.getTypeName());
    }

    StructObjectInspector soi = (StructObjectInspector) oi;
    List<? extends StructField> fields = soi.getAllStructFieldRefs();

    try {

        DataValueDescriptor dvd;
        for (int i = 0; i < fields.size(); i++) {
            StructField field = fields.get(i);
            dvd = row.getColumn(i + 1);
            ObjectInspector fieldOI = field.getFieldObjectInspector();
            Object fieldObj = soi.getStructFieldData(obj, field);
            PrimitiveObjectInspector primOI = (PrimitiveObjectInspector) fieldOI;
            Object data = primOI.getPrimitiveJavaObject(fieldObj);

            PrimitiveCategory primitiveCategory = primOI.getPrimitiveCategory();
            switch (primitiveCategory) {
            case BYTE:
                dvd.setValue(((Byte) data).byteValue());
                break;
            case INT:
                dvd.setValue(((Integer) data).intValue());
                break;
            case VARCHAR:
                dvd.setValue(((HiveVarchar) data).getValue());
                break;
            case CHAR:
                dvd.setValue(((HiveChar) data).getValue());
                break;
            case STRING:
                dvd.setValue((String) data);
                break;
            case BINARY:
                dvd.setValue((SerializationUtils.serialize((Serializable) data))); // is this right?  Should just be a byte[]
                break;
            case BOOLEAN:
                dvd.setValue(((Boolean) data).booleanValue());
                break;
            case DECIMAL:
                dvd.setValue(((HiveDecimal) data).doubleValue());
                break;
            case DOUBLE:
                dvd.setValue(((Double) data).doubleValue());
                break;
            case FLOAT:
                dvd.setValue(((Float) data).floatValue());
                break;
            case LONG:
                dvd.setValue(((Long) data).longValue());
                break;
            case SHORT:
                dvd.setValue(((Short) data).shortValue());
                break;
            case TIMESTAMP:
                dvd.setValue((Timestamp) data);
                break;
            case DATE:
                dvd.setValue((java.sql.Date) data);
                break;
            default:
                throw new SerDeException(
                        String.format("Hive Type %s Not Supported Yet", primOI.getPrimitiveCategory()));
            }
        }

    } catch (StandardException e) {
        // TODO Auto-generated catch block
        throw new RuntimeException("Serialized Object To Java Type Error");
    }
    ExecRowWritable rowWritable = new ExecRowWritable(
            WriteReadUtils.getExecRowFromTypeFormatIds(execRowFormatIds));
    rowWritable.set(row);
    return rowWritable;
}

From source file:at.gv.egovernment.moa.id.protocols.pvp2x.SingleLogOutAction.java

@Override
public SLOInformationInterface processRequest(IRequest req, HttpServletRequest httpReq,
        HttpServletResponse httpResp, IAuthData authData) throws MOAIDException {

    PVPTargetConfiguration pvpReq = (PVPTargetConfiguration) req;

    if (pvpReq.getRequest() instanceof MOARequest
            && ((MOARequest) pvpReq.getRequest()).getSamlRequest() instanceof LogoutRequest) {
        Logger.debug("Process Single LogOut request");
        MOARequest samlReq = (MOARequest) pvpReq.getRequest();
        LogoutRequest logOutReq = (LogoutRequest) samlReq.getSamlRequest();

        AuthenticationSession session = AuthenticationSessionStoreage.searchMOASessionWithNameIDandOAID(
                logOutReq.getIssuer().getValue(), logOutReq.getNameID().getValue());

        if (session == null) {
            Logger.warn("Can not find active SSO session with nameID " + logOutReq.getNameID().getValue()
                    + " and OA " + logOutReq.getIssuer().getValue());
            Logger.info("Search active SSO session with SSO session cookie");
            SSOManager ssomanager = SSOManager.getInstance();
            String ssoID = ssomanager.getSSOSessionID(httpReq);
            if (MiscUtil.isEmpty(ssoID)) {
                Logger.warn("Can not find active Session. Single LogOut not possible!");
                SingleLogoutService sloService = SingleLogOutBuilder.getResponseSLODescriptor(pvpReq);
                //LogoutResponse message = SingleLogOutBuilder.buildSLOErrorResponse(sloService, pvpReq, StatusCode.RESPONDER_URI);
                LogoutResponse message = SingleLogOutBuilder.buildSLOResponseMessage(sloService, pvpReq, null);
                Logger.info("Sending SLO success message to requester ...");
                SingleLogOutBuilder.sendFrontChannelSLOMessage(sloService, message, httpReq, httpResp,
                        samlReq.getRelayState());
                return null;

            } else {
                String moasession = ssomanager.getMOASession(ssoID);
                try {
                    session = AuthenticationSessionStoreage.getSession(moasession);

                } catch (MOADatabaseException e) {
                    Logger.warn("Can not find active Session. Single LogOut not possible!");
                    SingleLogoutService sloService = SingleLogOutBuilder.getResponseSLODescriptor(pvpReq);
                    //LogoutResponse message = SingleLogOutBuilder.buildSLOErrorResponse(sloService, pvpReq, StatusCode.RESPONDER_URI);
                    LogoutResponse message = SingleLogOutBuilder.buildSLOResponseMessage(sloService, pvpReq,
                            null);// w ww.  j a v a 2  s .  com
                    Logger.info("Sending SLO success message to requester ...");
                    SingleLogOutBuilder.sendFrontChannelSLOMessage(sloService, message, httpReq, httpResp,
                            samlReq.getRelayState());
                    return null;

                }
            }
        }

        AuthenticationManager authManager = AuthenticationManager.getInstance();
        authManager.performSingleLogOut(httpReq, httpResp, session, pvpReq);

    } else if (pvpReq.getRequest() instanceof MOAResponse
            && ((MOAResponse) pvpReq.getRequest()).getResponse() instanceof LogoutResponse) {
        Logger.debug("Process Single LogOut response");
        LogoutResponse logOutResp = (LogoutResponse) ((MOAResponse) pvpReq.getRequest()).getResponse();

        Transaction tx = null;

        try {
            String relayState = pvpReq.getRequest().getRelayState();
            if (MiscUtil.isEmpty(relayState)) {
                Logger.warn(
                        "SLO Response from " + logOutResp.getIssuer().getValue() + " has no SAML2 RelayState.");
                throw new SLOException("pvp2.19", null);

            }

            Session session = MOASessionDBUtils.getCurrentSession();
            boolean storageSuccess = false;
            int counter = 0;

            //TODO: add counter to prevent deadlock

            while (!storageSuccess) {
                tx = session.beginTransaction();

                List result;
                Query query = session.getNamedQuery("getAssertionWithArtifact");
                query.setParameter("artifact", relayState);
                result = query.list();
                Logger.trace("Found entries: " + result.size());

                //Assertion requires an unique artifact
                if (result.size() != 1) {
                    Logger.trace("No entries found.");
                    throw new MOADatabaseException("No sessioninformation found with this ID");
                }

                AssertionStore element = (AssertionStore) result.get(0);
                Object data = SerializationUtils.deserialize(element.getAssertion());

                if (data instanceof SLOInformationContainer) {
                    SLOInformationContainer sloContainer = (SLOInformationContainer) data;

                    //check status
                    SingleLogOutBuilder.checkStatusCode(sloContainer, logOutResp);

                    if (sloContainer.hasFrontChannelOA()) {
                        try {
                            //some response are open
                            byte[] serializedSLOContainer = SerializationUtils
                                    .serialize((Serializable) sloContainer);
                            element.setAssertion(serializedSLOContainer);
                            element.setType(sloContainer.getClass().getName());

                            session.saveOrUpdate(element);
                            tx.commit();

                            //sloContainer could be stored to database
                            storageSuccess = true;

                        } catch (HibernateException e) {
                            tx.rollback();

                            counter++;
                            Logger.debug(
                                    "SLOContainter could not stored to database. Wait some time and restart storage process ... ");
                            java.util.Random rand = new java.util.Random();

                            try {
                                Thread.sleep(rand.nextInt(20) * 10);

                            } catch (InterruptedException e1) {
                                Logger.warn("Thread could not stopped. ReStart storage process immediately",
                                        e1);
                            }
                        }

                    } else {
                        //last response received.
                        try {
                            session.delete(element);
                            tx.commit();

                        } catch (HibernateException e) {
                            tx.rollback();
                            Logger.error("SLOContainter could not deleted from database. ");

                        }

                        storageSuccess = true;
                        String redirectURL = null;
                        if (sloContainer.getSloRequest() != null) {
                            //send SLO response to SLO request issuer
                            SingleLogoutService sloService = SingleLogOutBuilder
                                    .getResponseSLODescriptor(sloContainer.getSloRequest());
                            LogoutResponse message = SingleLogOutBuilder.buildSLOResponseMessage(sloService,
                                    sloContainer.getSloRequest(), sloContainer.getSloFailedOAs());
                            redirectURL = SingleLogOutBuilder.getFrontChannelSLOMessageURL(sloService, message,
                                    httpReq, httpResp,
                                    sloContainer.getSloRequest().getRequest().getRelayState());

                        } else {
                            //print SLO information directly
                            redirectURL = AuthConfigurationProvider.getInstance().getPublicURLPrefix()
                                    + "/idpSingleLogout";

                            String artifact = Random.nextRandom();

                            String statusCode = null;
                            if (sloContainer.getSloFailedOAs() == null
                                    || sloContainer.getSloFailedOAs().size() == 0)
                                statusCode = SLOSTATUS_SUCCESS;
                            else
                                statusCode = SLOSTATUS_ERROR;

                            AssertionStorage.getInstance().put(artifact, statusCode);
                            redirectURL = addURLParameter(redirectURL, PARAM_SLOSTATUS, artifact);

                        }
                        //redirect to Redirect Servlet
                        String url = AuthConfigurationProvider.getInstance().getPublicURLPrefix()
                                + "/RedirectServlet";
                        url = addURLParameter(url, RedirectServlet.REDIRCT_PARAM_URL,
                                URLEncoder.encode(redirectURL, "UTF-8"));
                        url = httpResp.encodeRedirectURL(url);

                        httpResp.setContentType("text/html");
                        httpResp.setStatus(302);
                        httpResp.addHeader("Location", url);

                    }
                } else {
                    Logger.warn("Sessioninformation Cast-Exception by using Artifact=" + relayState);
                    throw new MOADatabaseException("Sessioninformation Cast-Exception");

                }
            }

        } catch (MOADatabaseException e) {
            Logger.error("MOA AssertionDatabase ERROR", e);
            throw new SLOException("pvp2.19", null);

        } catch (UnsupportedEncodingException e) {
            Logger.error("Finale SLO redirct not possible.", e);
            throw new AuthenticationException("pvp2.13", new Object[] {});

        } finally {
            if (tx != null && !tx.wasCommitted()) {
                tx.commit();

            }
        }

    } else {
        Logger.error("Process SingleLogOutAction but request is NOT of type LogoutRequest or LogoutResponse.");
        throw new MOAIDException("pvp2.13", null);

    }

    return null;
}

From source file:fr.inria.oak.paxquery.translation.Logical2Pact.java

private static final Operator<Record>[] translate(Projection proj) {
    Operator<Record>[] childPlan = translate(proj.getChild());

    // create MapOperator for projecting a column
    MapOperator projection = MapOperator.builder(ProjectionOperator.class).input(childPlan).name("Proj")
            .build();//from w  ww  .  jav a  2  s. co m

    // projection configuration
    final String encodedNRSMD = DatatypeConverter
            .printBase64Binary(SerializationUtils.serialize(proj.getNRSMD()));
    projection.setParameter(PACTOperatorsConfiguration.NRSMD1_BINARY.toString(), encodedNRSMD);
    final String encodedKeepColumns = DatatypeConverter
            .printBase64Binary(SerializationUtils.serialize(proj.columns));
    projection.setParameter(PACTOperatorsConfiguration.KEEP_COLUMNS_BINARY.toString(), encodedKeepColumns);

    return new Operator[] { projection };
}

From source file:com.impetus.ankush.common.domain.Cluster.java

@Transient
public void setClusterConf(ClusterConfig clusterConf) {
    setConfBytes(SerializationUtils.serialize(clusterConf));
}

From source file:io.pravega.segmentstore.server.host.ZKSegmentContainerMonitorTest.java

@Test
public void testRetryOnExceptions() throws Exception {
    @Cleanup/*from w w  w . j  a  v a  2s .  c om*/
    CuratorFramework zkClient = startClient();
    initializeHostContainerMapping(zkClient);

    SegmentContainerRegistry containerRegistry = createMockContainerRegistry();
    @Cleanup
    ZKSegmentContainerMonitor segMonitor = createContainerMonitor(containerRegistry, zkClient);
    segMonitor.initialize(Duration.ofSeconds(1));

    // Simulate a container that throws exception on start.
    when(containerRegistry.startContainer(eq(2), any())).thenThrow(new RuntimeException());

    // Use ZK to send that information to the Container Manager.
    HashMap<Host, Set<Integer>> currentData = deserialize(zkClient, PATH);
    currentData.put(PRAVEGA_SERVICE_ENDPOINT, Collections.singleton(2));
    zkClient.setData().forPath(PATH, SerializationUtils.serialize(currentData));

    // Verify that it does not start.
    verify(containerRegistry, timeout(1000).atLeastOnce()).startContainer(eq(2), any());
    assertEquals(0, segMonitor.getRegisteredContainers().size());

    // Now simulate success for the same container.
    ContainerHandle containerHandle = mock(ContainerHandle.class);
    when(containerHandle.getContainerId()).thenReturn(2);
    when(containerRegistry.startContainer(eq(2), any()))
            .thenReturn(CompletableFuture.completedFuture(containerHandle));

    // Verify that it retries and starts the same container again.
    verify(containerRegistry, timeout(1000).atLeastOnce()).startContainer(eq(2), any());

    // Using wait here to ensure the private data structure is updated.
    // TODO: Removing dependency on sleep here and other places in this class
    // - https://github.com/pravega/pravega/issues/1079
    Thread.sleep(2000);
    assertEquals(1, segMonitor.getRegisteredContainers().size());
}

From source file:com.janrain.backplane2.server.V2MessageProcessor.java

private String processSingleMessage(BackplaneMessage backplaneMessage, Transaction transaction,
        List<String> insertionTimes, Pair<String, Date> lastIdAndDate) throws Exception {

    try {/*from  w  ww  .ja  v a2  s  . c  o  m*/
        String oldId = backplaneMessage.getIdValue();
        insertionTimes.add(oldId);

        // TOTAL ORDER GUARANTEE
        // verify that the date portion of the new message ID is greater than all existing message ID dates
        // if not, uptick id by 1 ms and insert
        // this means that all message ids have unique time stamps, even if they
        // arrived at the same time.

        backplaneMessage.updateId(lastIdAndDate);
        String newId = backplaneMessage.getIdValue();

        // messageTime is guaranteed to be a unique identifier of the message
        // because of the TOTAL ORDER mechanism above
        long messageTime = BackplaneMessage.getDateFromId(newId).getTime();

        // <ATOMIC>
        // save the individual message by key & TTL
        transaction.setex(RedisBackplaneMessageDAO.getKey(newId),
                DateTimeUtils.getExpireSeconds(backplaneMessage.getIdValue(),
                        backplaneMessage.get(BackplaneMessage.Field.EXPIRE), backplaneMessage.isSticky()),
                SerializationUtils.serialize(backplaneMessage));

        // channel and bus sorted set index
        transaction.zadd(RedisBackplaneMessageDAO.getChannelKey(backplaneMessage.getChannel()), messageTime,
                backplaneMessage.getIdValue().getBytes());
        transaction.zadd(RedisBackplaneMessageDAO.getBusKey(backplaneMessage.getBus()), messageTime,
                backplaneMessage.getIdValue().getBytes());

        // add message id to sorted set of all message ids as an index
        String metaData = backplaneMessage.getBus() + " " + backplaneMessage.getChannel() + " "
                + backplaneMessage.getIdValue();

        transaction.zadd(RedisBackplaneMessageDAO.V2_MESSAGES.getBytes(), messageTime, metaData.getBytes());

        // add message id to sorted set keyed by bus as an index

        // make sure all subscribers get the update
        transaction.publish("alerts", newId);

        // pop one message off the queue - which will only happen if this transaction is successful
        transaction.lpop(RedisBackplaneMessageDAO.V2_MESSAGE_QUEUE);
        // </ATOMIC>

        logger.info("pipelined v2 message " + oldId + " -> " + newId);
        return newId;
    } catch (Exception e) {
        throw e;
    }

}

From source file:fr.inria.oak.paxquery.translation.Logical2Pact.java

private static final Operator<Record>[] translate(Navigation nav) {
    Operator<Record>[] childPlan = translate(nav.getChild());

    // create MapOperator for navigating in a column
    MapOperator navigation = MapOperator.builder(NavigationOperator.class).input(childPlan).name("TPNav")
            .build();//  w w w. ja  v a  2s.co m

    // navigation configuration
    final String encodedNRSMD = DatatypeConverter
            .printBase64Binary(SerializationUtils.serialize(nav.getNRSMD()));
    navigation.setParameter(PACTOperatorsConfiguration.NRSMD1_BINARY.toString(), encodedNRSMD);
    navigation.setParameter(PACTOperatorsConfiguration.NAVIGATION_COLUMN_INT.toString(), nav.pos);
    navigation.setParameter(PACTOperatorsConfiguration.NTP_STRING.toString(),
            NavigationTreePatternUtils.getParsableStringFromTreePattern(nav.navigationTreePattern));

    return new Operator[] { navigation };
}

From source file:fr.inria.oak.paxquery.translation.Logical2Pact.java

private static final Operator<Record>[] translate(Flatten flat) {
    Operator<Record>[] childPlan = translate(flat.getChild());

    // create MapOperator to flatten tuples
    MapOperator flatten = MapOperator.builder(FlattenOperator.class).input(childPlan).name("Flatten").build();

    // flatten configuration
    final String encodedFlattenNRSMD = DatatypeConverter
            .printBase64Binary(SerializationUtils.serialize(flat.getNRSMD()));
    flatten.setParameter(PACTOperatorsConfiguration.NRSMD1_BINARY.toString(), encodedFlattenNRSMD);
    final String encodedUnnestPath = DatatypeConverter
            .printBase64Binary(SerializationUtils.serialize(flat.getUnnestPath()));
    flatten.setParameter(PACTOperatorsConfiguration.UNNEST_PATH_BINARY.toString(), encodedUnnestPath);

    return new Operator[] { flatten };
}

From source file:com.feedzai.fos.impl.weka.WekaManager.java

@Override
public Model train(ModelConfig config, List<Object[]> instances) throws FOSException {
    checkNotNull(instances, "Instances must be supplied");
    checkNotNull(config, "Config must be supplied");
    long time = System.currentTimeMillis();
    WekaModelConfig wekaModelConfig = new WekaModelConfig(config, wekaManagerConfig);
    Classifier classifier = WekaClassifierFactory.create(config);
    FastVector attributes = WekaUtils.instanceFields2Attributes(wekaModelConfig.getClassIndex(),
            config.getAttributes());//from   w  w w .ja  va 2 s. c  o  m
    InstanceSetter[] instanceSetters = WekaUtils.instanceFields2ValueSetters(config.getAttributes(),
            InstanceType.TRAINING);

    Instances wekaInstances = new Instances(config.getProperty(WekaModelConfig.CLASSIFIER_IMPL), attributes,
            instances.size());

    for (Object[] objects : instances) {
        wekaInstances.add(WekaUtils.objectArray2Instance(objects, instanceSetters, attributes));
    }

    trainClassifier(wekaModelConfig.getClassIndex(), classifier, wekaInstances);

    final byte[] bytes = SerializationUtils.serialize(classifier);

    logger.debug("Trained model with {} instances in {}ms", instances.size(),
            (System.currentTimeMillis() - time));

    return new ModelBinary(bytes);
}