//###
//# #%L
//# mosaic-java-sun-utilities
//# %%
//# Copyright (C) 2010 - 2012 Seconda Università di Napoli
//#Authors: Massimiliano Rak, Giuseppe Aversano
//# %%
//# 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.
//# #L%
//###
package eu.mosaic_cloud.sunutilities;
import eu.mosaic_cloud.cloudlets.connectors.kvstore.IKvStoreConnector;
import eu.mosaic_cloud.cloudlets.connectors.kvstore.IKvStoreConnectorFactory;
import eu.mosaic_cloud.cloudlets.connectors.kvstore.KvStoreCallbackCompletionArguments;
import eu.mosaic_cloud.cloudlets.connectors.queue.amqp.AmqpQueueConsumeCallbackArguments;
import eu.mosaic_cloud.cloudlets.connectors.queue.amqp.IAmqpQueueConsumerConnector;
import eu.mosaic_cloud.cloudlets.connectors.queue.amqp.IAmqpQueueConsumerConnectorFactory;
import eu.mosaic_cloud.cloudlets.connectors.queue.amqp.IAmqpQueuePublisherConnector;
import eu.mosaic_cloud.cloudlets.connectors.queue.amqp.IAmqpQueuePublisherConnectorFactory;
import eu.mosaic_cloud.cloudlets.core.CallbackArguments;
import eu.mosaic_cloud.cloudlets.core.CloudletCallbackArguments;
import eu.mosaic_cloud.cloudlets.core.CloudletCallbackCompletionArguments;
import eu.mosaic_cloud.cloudlets.core.GenericCallbackCompletionArguments;
import eu.mosaic_cloud.cloudlets.core.ICallback;
import eu.mosaic_cloud.cloudlets.core.ICloudletController;
import eu.mosaic_cloud.cloudlets.tools.DefaultAmqpPublisherConnectorCallback;
import eu.mosaic_cloud.cloudlets.tools.DefaultAmqpQueueConsumerConnectorCallback;
import eu.mosaic_cloud.cloudlets.tools.DefaultCloudletCallback;
import eu.mosaic_cloud.cloudlets.tools.DefaultKvStoreConnectorCallback;
import eu.mosaic_cloud.platform.core.configuration.ConfigUtils;
import eu.mosaic_cloud.platform.core.configuration.ConfigurationIdentifier;
import eu.mosaic_cloud.platform.core.configuration.IConfiguration;
import eu.mosaic_cloud.platform.core.utils.NullDataEncoder;
import eu.mosaic_cloud.platform.core.utils.PlainTextDataEncoder;
import eu.mosaic_cloud.platform.core.utils.SerializedDataEncoder;
import eu.mosaic_cloud.tools.callbacks.core.CallbackCompletion;
import eu.mosaic_cloud.platform.core.utils.JsonDataEncoder;
import java.util.UUID;
import java.util.Vector;
import java.io.*;
import org.json.JSONObject;
import org.json.JSONException;
import com.google.gson.Gson;
public class Servlet
{
//KvStoreByte
public static final class KeyValueCallbackbyte
extends DefaultKvStoreConnectorCallback<ServletContext, byte[], UUID>
{
@Override
public CallbackCompletion<Void> destroySucceeded (final ServletContext context, final CallbackArguments arguments)
{
this.logger.info ("KeyValue Input destroyed successfully.");
return ICallback.SUCCESS;
}
@Override
public CallbackCompletion<Void> initializeSucceeded (final ServletContext context, final CallbackArguments arguments)
{
this.logger.info ("KeyValue Input initialized successfully");
return ICallback.SUCCESS;
}
public CallbackCompletion<Void> setSucceeded(final ServletContext context, final KvStoreCallbackCompletionArguments<byte[],UUID> arguments) {
this.logger.info("\n\nSet nel KV Store Input Succeeded\n\n");
String k=arguments.getKey();
this.logger.info("\n\nKEY:"+k);
this.logger.info("\n\n");
byte[] valueb=(byte[])arguments.getValue();
String value=new String(valueb);
this.logger.info("\n\nResultati in byte:"+valueb);
this.logger.info("\n\nResultati in String:"+value);
this.logger.info("\n\n");
context.keymes.setKey(k);
context.publisher.publish(context.keymes,null);
return ICallback.SUCCESS;
}
public CallbackCompletion<Void> deleteSucceeded (final ServletContext context, final CallbackArguments arguments)
{
this.logger.info ("KeyValue Input delete successfully.");
return ICallback.SUCCESS;
}
public CallbackCompletion<Void> getSucceeded(final ServletContext context,final KvStoreCallbackCompletionArguments<byte[],UUID> arguments) {
this.logger.info("\n\nGet nel KV Store Input Succeeded\n\n");
String key =(String) arguments.getKey();
byte[] value=(byte[])arguments.getValue();
return ICallback.SUCCESS;
}
public CallbackCompletion<Void> ungetSucceeded (final ServletContext context, final CallbackArguments arguments)
{
this.logger.info ("KeyValue Input ungetSucceeded.");
return ICallback.SUCCESS;
}
}
//Push verso MatlabOneCloudlet
public static final class AmqpPublisherCallback
extends DefaultAmqpPublisherConnectorCallback<ServletContext, KeyMessage, Void>
{
@Override
public CallbackCompletion<Void> destroySucceeded (final ServletContext context, final CallbackArguments arguments)
{
this.logger.info ("Servlet publisher destroyed successfully.");
return ICallback.SUCCESS;
}
@Override
public CallbackCompletion<Void> initializeSucceeded (final ServletContext context, final CallbackArguments arguments)
{
this.logger.info ("Servlet publisher initialized successfully.");
return ICallback.SUCCESS;
}
public CallbackCompletion<Void> publishSucceeded (final ServletContext context, final GenericCallbackCompletionArguments<KeyMessage> arguments)
{
this.logger.info ("Servlet publish successfully");
//context.cloudlet.destroy ();
return ICallback.SUCCESS;
}
}
//Consumer mhttpgw
public static final class AmqpConsumerCallback
extends DefaultAmqpQueueConsumerConnectorCallback<ServletContext, byte[], Void>
{
@Override
public CallbackCompletion<Void> consume (final ServletContext context, final AmqpQueueConsumeCallbackArguments<byte[]> arguments)
{
final byte[] val = arguments.getMessage ();
this.logger.info ("\nConsumerServlet received message Byte:"+val);
String data = new String(val);
this.logger.info ("\nConsumerServlet received message String:"+data);
context.instanceID=UUID.randomUUID();
//context.keymes.setKey(context.key);
JSONObject json = null;
try {
json = new JSONObject(data);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
context.kvStorebyte.set(context.key, json.toString().getBytes() , context.instanceID);
context.consumer.acknowledge (arguments.getToken ());
return ICallback.SUCCESS;
}
@Override
public CallbackCompletion<Void> destroySucceeded (final ServletContext context, final CallbackArguments arguments)
{
this.logger.info ("ConsumerServlet destroyed successfully.");
return ICallback.SUCCESS;
}
@Override
public CallbackCompletion<Void> initializeSucceeded (final ServletContext context, final CallbackArguments arguments)
{
this.logger.info ("ConsumerServlet initialized successfully.");
return ICallback.SUCCESS;
}
public CallbackCompletion<Void> acknowledgeSucceeded (final ServletContext context, final GenericCallbackCompletionArguments<Void> arguments)
{
// context.cloudlet.destroy ();
return ICallback.SUCCESS;
}
}
public static final class LifeCycleHandler
extends DefaultCloudletCallback<ServletContext>
{
@Override
public CallbackCompletion<Void> destroy (final ServletContext context, final CloudletCallbackArguments<ServletContext> arguments)
{
this.logger.info ("Servlet destroying...");
return CallbackCompletion.createAndChained (context.kvStorebyte.destroy (), context.publisher.destroy(),context.consumer.destroy());
}
@Override
public CallbackCompletion<Void> destroySucceeded (final ServletContext context, final CloudletCallbackCompletionArguments<ServletContext> arguments)
{
this.logger.info ("Servlet destroyed successfully.");
return ICallback.SUCCESS;
}
@Override
public CallbackCompletion<Void> initialize (final ServletContext context, final CloudletCallbackArguments<ServletContext> arguments)
{
this.logger.info ("Servlet initializing...");
context.cloudlet = arguments.getCloudlet ();
final IConfiguration configuration = context.cloudlet.getConfiguration ();
final IConfiguration queueConfiguration2 = configuration.spliceConfiguration (ConfigurationIdentifier.resolveAbsolute ("queue_input.publisher"));
context.publisher = context.cloudlet.getConnectorFactory (IAmqpQueuePublisherConnectorFactory.class).create (queueConfiguration2, KeyMessage.class, JsonDataEncoder.create(KeyMessage.class), new AmqpPublisherCallback(), context);
final IConfiguration queueConfiguration = configuration.spliceConfiguration (ConfigurationIdentifier.resolveAbsolute ("queuemhttpgw.consumer"));
context.consumer = context.cloudlet.getConnectorFactory (IAmqpQueueConsumerConnectorFactory.class).create (queueConfiguration, byte[].class, NullDataEncoder.create(), new AmqpConsumerCallback (), context);
final IConfiguration kvConfigurationbyte = configuration.spliceConfiguration (ConfigurationIdentifier.resolveAbsolute ("resultstorebyte"));
context.kvStorebyte = context.cloudlet.getConnectorFactory (IKvStoreConnectorFactory.class).create (kvConfigurationbyte, byte[].class, NullDataEncoder.DEFAULT_INSTANCE, new KeyValueCallbackbyte (), context);
return CallbackCompletion.createAndChained (context.kvStorebyte.initialize(),context.publisher.initialize(),context.consumer.initialize());
}
@Override
public CallbackCompletion<Void> initializeSucceeded (final ServletContext context, final CloudletCallbackCompletionArguments<ServletContext> arguments)
{
this.logger.info ("Servlet initialized successfully.");
context.keymes= new KeyMessage();
return ICallback.SUCCESS;
}
}
public static final class ServletContext
{
ICloudletController<ServletContext> cloudlet;
IAmqpQueuePublisherConnector<KeyMessage, Void> publisher;
IAmqpQueueConsumerConnector<byte[], Void> consumer;
IKvStoreConnector<byte[], UUID> kvStorebyte;
String key="matlab";
UUID instanceID=null;
KeyMessage keymes;
String matrice="{'my_elementov_lks':'0\t0\tUnknown\n','mx_1':'1\t2\tInteger\n0\t0\n','material_1':'1\t2\tInteger\n100\t100\n','my_1':'1\t2\tInteger\n0\t0\n','nx_1':'1\t2\tInteger\n0\t0\n','elementi':'5\t3\tInteger\n1\t1\t2\n2\t2\t3\n3\t3\t4\n4\t4\t5\n5\t5\t6\n','n_mesh_1':'1\t1\tInteger\n40\n','podpora_1':'1\t6\tInteger\n1\t1\t1\t1\t1\t1\n','nZ_elementov_gks':'0\t0\tUnknown\n','mZ_elementov_gks':'0\t0\tUnknown\n','nX_elementov_gks':'0\t0\tUnknown\n','tocke':'6\t4\tInteger\n1\t0\t0\t0\n2\t2\t0\t0\n3\t4\t0\t0\n4\t6\t0\t0\n5\t8\t0\t0\n6\t10\t0\t0\n','ny_elementov_lks':'0\t0\tUnknown\n','nY_elementov_gks':'0\t0\tUnknown\n','obtezba_vozlisc':'1\t7\tDouble\n6\t0\t5\t0\t0\t62.8319\t0\n','material_elementov':'5\t3\tInteger\n1\t100\t100\n2\t100\t100\n3\t100\t100\n4\t100\t100\n5\t100\t100\n','sprostitev_1':'6\t2\tInteger\n0\t0\n0\t0\n0\t0\n0\t0\n0\t0\n0\t0\n','mesh_el':'5\t2\tInteger\n1\t40\n2\t40\n3\t40\n4\t40\n5\t40\n','tockovna_obtezba_1':'1\t6\tDouble\n0\t5\t0\t0\t62.8319\t0\n','podatki_za_statiko':'2\t1\tDouble\n2\n1e-10\n','nz_elementov_lks':'0\t0\tUnknown\n','mx_elementov_lks':'0\t0\tUnknown\n','podpore_vozlisc':'1\t7\tInteger\n1\t1\t1\t1\t1\t1\t1\n','mX_elementov_gks':'0\t0\tUnknown\n','geometrija_elementov':'5\t7\tInteger\n1\t100\t100\t100\t1\t1\t1\n2\t100\t100\t100\t1\t1\t1\n3\t100\t100\t100\t1\t1\t1\n4\t100\t100\t100\t1\t1\t1\n5\t100\t100\t100\t1\t1\t1\n','mY_elementov_gks':'0\t0\tUnknown\n','geometrija_prereza_1':'1\t6\tInteger\n100\t100\t100\t1\t1\t1\n','materialni_model':'1\t1\tInteger\n1\n','sprostitve_elementov':'0\t0\tUnknown\n','ny_1':'1\t2\tInteger\n0\t0\n','mz_elementov_lks':'0\t0\tUnknown\n','nx_elementov_lks':'0\t0\tUnknown\n','mz_1':'1\t2\tInteger\n0\t0\n','nz_1':'1\t2\tInteger\n0\t0\n'}";
}
}