Java tutorial
/* * Copyright (C) 2014 SETRONICA - setronica.com * This source code is available under the terms of the GNU Lesser General Public License * as published by The Open Source Initiative (OSI), either version 3 of the License, * or (at your option) any later version. * * UCS is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * * See the GNU Lesser General Public License for more details. * You should have received a copy of the GNU Lesser General Public License. */ package com.setronica.ucs.server; import com.setronica.ucs.client.ServiceOperation; import com.setronica.ucs.client.ServiceOperationResult; import com.setronica.ucs.locale.ErrorCode; import com.setronica.ucs.locale.LocalizableException; import com.setronica.ucs.locale.Message; import com.setronica.ucs.protocol.Operation; import com.setronica.ucs.protocol.OperationResult; import com.setronica.ucs.service.RemoteService; import de.ruedigermoeller.serialization.FSTConfiguration; import de.ruedigermoeller.serialization.FSTObjectInput; import de.ruedigermoeller.serialization.FSTObjectOutput; import org.springframework.context.ApplicationContext; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.Serializable; public class MsgPackUCSCoreService /*implements UCSCoreService*/ { private ApplicationContext applicationContext; private FSTConfiguration fstConfiguration; public MsgPackUCSCoreService(ApplicationContext applicationContext) { this.applicationContext = applicationContext; fstConfiguration = FSTConfiguration.createDefaultConfiguration(); } public ServiceOperationResult execute(ServiceOperation serviceOperation) throws LocalizableException { RemoteService remoteService = (RemoteService) applicationContext.getBean(serviceOperation.serviceName); try { Operation operation = deserialize(serviceOperation.operation); OperationResult operationResult = remoteService.execute(operation); ServiceOperationResult serviceOperationResult = new ServiceOperationResult(); serviceOperationResult.result = serialize(operationResult); return serviceOperationResult; } catch (ClassNotFoundException e) { throw new LocalizableException(ErrorCode.Service_UnknownOperation, new Message("unexpected.operation.0", e.getMessage())); } catch (Exception e) { throw new LocalizableException(e, ErrorCode.InternalError, new Message("operation.execution.exception")); } } private byte[] serialize(Serializable obj) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); // ObjectOutputStream objectOutputStream = new ObjectOutputStream(out); FSTObjectOutput objectOutputStream = new FSTObjectOutput(out, fstConfiguration); objectOutputStream.writeObject(obj); objectOutputStream.close(); return out.toByteArray(); } private Operation deserialize(byte[] operation) throws ClassNotFoundException, IOException { // ObjectInputStream objectInputStream = new ObjectInputStream(new ByteArrayInputStream(operation)); FSTObjectInput objectInputStream = new FSTObjectInput(new ByteArrayInputStream(operation), fstConfiguration); return (Operation) objectInputStream.readObject(); } }