//###
//# #%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 com.google.gson.Gson;
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.platform.core.configuration.ConfigurationIdentifier;
import eu.mosaic_cloud.platform.core.configuration.IConfiguration;
import eu.mosaic_cloud.platform.core.utils.JsonDataEncoder;
import eu.mosaic_cloud.platform.core.utils.NullDataEncoder;
import eu.mosaic_cloud.platform.core.utils.PlainTextDataEncoder;
import eu.mosaic_cloud.tools.callbacks.core.CallbackCompletion;
public class ConsumerBytepdf
{
public static final class UrlPublisherCallback
extends DefaultAmqpPublisherConnectorCallback<ConsumerBytepdfContext, FileMessage, Void>
{
@Override
public CallbackCompletion<Void> destroySucceeded (final ConsumerBytepdfContext context, final CallbackArguments arguments)
{
this.logger.info ("ConsumerBytepdfConsumer destroyed successfully.");
return ICallback.SUCCESS;
}
@Override
public CallbackCompletion<Void> initializeSucceeded (final ConsumerBytepdfContext context, final CallbackArguments arguments)
{
this.logger.info ("ConsumerBytepdfConsumer initialized successfully.");
return ICallback.SUCCESS;
}
}
public static final class AmqpConsumerCallback
extends DefaultAmqpQueueConsumerConnectorCallback<ConsumerBytepdfContext, byte[], Void>
{
@Override
public CallbackCompletion<Void> consume (final ConsumerBytepdfContext context, final AmqpQueueConsumeCallbackArguments<byte[]> arguments)
{
final byte[] val = arguments.getMessage ();
this.logger.info ("\n\nConsumerBytepdfConsumer received message Byte:"+val);
String data = new String(val);
this.logger.info ("\n\nConsumerBytepdfConsumer received message String:"+data);
context.object=context.gson.fromJson(data,JsonObjectfromString.class);
context.count++;
context.Mess= new FileMessage(context.object.getParameters(1),""+context.count,""+context.count+".pdf","{'httpg':{'end-timestamp':'2013-02-19T13:36:03.462+0200','start-timestamp':'2013-02-19T13:36:03.384+0200'}}");
context.receivedUrls.publish(context.Mess, null);
context.consumer.acknowledge (arguments.getToken ());
return ICallback.SUCCESS;
}
@Override
public CallbackCompletion<Void> destroySucceeded (final ConsumerBytepdfContext context, final CallbackArguments arguments)
{
this.logger.info ("ConsumerBytepdfConsumer destroyed successfully.");
return ICallback.SUCCESS;
}
@Override
public CallbackCompletion<Void> initializeSucceeded (final ConsumerBytepdfContext context, final CallbackArguments arguments)
{
this.logger.info ("ConsumerBytepdfConsumer initialized successfully.");
return ICallback.SUCCESS;
}
public CallbackCompletion<Void> acknowledgeSucceeded (final ConsumerBytepdfContext context, final GenericCallbackCompletionArguments<Void> arguments)
{
// context.cloudlet.destroy ();
return ICallback.SUCCESS;
}
}
public static final class LifeCycleHandler
extends DefaultCloudletCallback<ConsumerBytepdfContext>
{
@Override
public CallbackCompletion<Void> destroy (final ConsumerBytepdfContext context, final CloudletCallbackArguments<ConsumerBytepdfContext> arguments)
{
this.logger.info ("ConsumerBytepdf is being destroyed...");
return CallbackCompletion.createAndChained (context.consumer.destroy (),context.receivedUrls.destroy());
}
@Override
public CallbackCompletion<Void> destroySucceeded (final ConsumerBytepdfContext context, final CloudletCallbackCompletionArguments<ConsumerBytepdfContext> arguments)
{
this.logger.info ("ConsumerBytepdf was destroyed successfully.");
return ICallback.SUCCESS;
}
@Override
public CallbackCompletion<Void> initialize (final ConsumerBytepdfContext context, final CloudletCallbackArguments<ConsumerBytepdfContext> arguments)
{
this.logger.info ("ConsumerBytepdf initializing...");
context.cloudlet = arguments.getCloudlet ();
final IConfiguration configuration = context.cloudlet.getConfiguration ();
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);
ICloudletController<ConsumerBytepdfContext> cloudlet = arguments.getCloudlet();
context.cloudlet = cloudlet;
IConfiguration cloudletConfiguration = cloudlet.getConfiguration ();
IConfiguration publisherConfiguration = cloudletConfiguration.spliceConfiguration( ConfigurationIdentifier.resolveAbsolute( "queue.publisher" ) );
context.receivedUrls = cloudlet.getConnectorFactory( IAmqpQueuePublisherConnectorFactory.class ).create(
publisherConfiguration,
FileMessage.class,
JsonDataEncoder.create( FileMessage.class ),
new UrlPublisherCallback(), context );
return CallbackCompletion.createAndChained (context.consumer.initialize (),context.receivedUrls.initialize ());
}
@Override
public CallbackCompletion<Void> initializeSucceeded (final ConsumerBytepdfContext context, final CloudletCallbackCompletionArguments<ConsumerBytepdfContext> arguments)
{
this.logger.info ("ConsumerByte initialized successfully.");
context.object=new JsonObjectfromString();
context.gson=new Gson();
return ICallback.SUCCESS;
}
}
public static final class ConsumerBytepdfContext extends ContextWithStatusStore
{
ICloudletController<ConsumerBytepdfContext> cloudlet;
IAmqpQueueConsumerConnector<byte[], Void> consumer;
IAmqpQueuePublisherConnector<FileMessage, Void> receivedUrls;
FileMessage Mess;
public Gson gson;
public JsonObjectfromString object;
int count=0;
}
}