From 74514087b3445ac0eb2bed056fc6c0b404b36092 Mon Sep 17 00:00:00 2001 From: Igor Khanenko Date: Tue, 10 Feb 2015 21:54:14 +0200 Subject: [PATCH] KAA-343: Updated java sdk generation and added notification deserializer template. --- .../control/service/sdk/JavaSdkGenerator.java | 34 ++++++-- ...AbstractNotificationListener.java.template | 40 --------- .../AbstractProfileContainer.java.template | 84 ------------------- .../nf/NotificationDeserializer.java.template | 23 +++++ .../nf/NotificationListener.java.template | 38 +++++++++ 5 files changed, 87 insertions(+), 132 deletions(-) delete mode 100644 server/control/src/main/resources/sdk/java/AbstractNotificationListener.java.template delete mode 100644 server/control/src/main/resources/sdk/java/AbstractProfileContainer.java.template create mode 100644 server/control/src/main/resources/sdk/java/nf/NotificationDeserializer.java.template create mode 100644 server/control/src/main/resources/sdk/java/nf/NotificationListener.java.template diff --git a/server/control/src/main/java/org/kaaproject/kaa/server/control/service/sdk/JavaSdkGenerator.java b/server/control/src/main/java/org/kaaproject/kaa/server/control/service/sdk/JavaSdkGenerator.java index a6e9625dcd..a0b8d4fe79 100644 --- a/server/control/src/main/java/org/kaaproject/kaa/server/control/service/sdk/JavaSdkGenerator.java +++ b/server/control/src/main/java/org/kaaproject/kaa/server/control/service/sdk/JavaSdkGenerator.java @@ -120,6 +120,12 @@ public class JavaSdkGenerator extends SdkGenerator { /** The Constant EVENT_CLASS_FAMILY_VERSION_PROPERTY. */ private static final String EVENT_CLASS_FAMILY_VERSION_PROPERTY = "event_cf_version"; + /** The Constant NOTIFICATION_LISTENER_SOURCE_TEMPLATE. */ + private static final String NOTIFICATION_LISTENER_SOURCE_TEMPLATE = "sdk/java/nf/NotificationListener.java.template"; + + /** The Constant NOTIFICATION_DESERIALIZER_SOURCE_TEMPLATE. */ + private static final String NOTIFICATION_DESERIALIZER_SOURCE_TEMPLATE = "sdk/java/nf/NotificationDeserializer.java.template"; + /** The Constant PROFILE_CONTAINER_SOURCE_TEMPLATE. */ private static final String PROFILE_CONTAINER_SOURCE_TEMPLATE = "sdk/java/profile/ProfileContainer.java.template"; @@ -129,9 +135,6 @@ public class JavaSdkGenerator extends SdkGenerator { /** The Constant DEFAULT_PROFILE_SERIALIZER_SOURCE_TEMPLATE. */ private static final String DEFAULT_PROFILE_SERIALIZER_SOURCE_TEMPLATE = "sdk/java/profile/DefaultProfileSerializer.java.template"; - /** The Constant ABSTRACT_NOTIFICATION_LISTENER_SOURCE_TEMPLATE. */ - private static final String ABSTRACT_NOTIFICATION_LISTENER_SOURCE_TEMPLATE = "sdk/java/AbstractNotificationListener.java.template"; - /** The Constant LOG_RECORD_SOURCE_TEMPLATE. */ private static final String LOG_RECORD_SOURCE_TEMPLATE = "sdk/java/log/LogRecord.java.template"; @@ -150,8 +153,11 @@ public class JavaSdkGenerator extends SdkGenerator { /** The Constant ABSTRACT_PROFILE_CONTAINER. */ private static final String PROFILE_SERIALIZER = "ProfileSerializer"; - /** The Constant ABSTRACT_NOTIFICATION_LISTENER. */ - private static final String ABSTRACT_NOTIFICATION_LISTENER = "AbstractNotificationListener"; + /** The Constant NOTIFICATION_LISTENER. */ + private static final String NOTIFICATION_LISTENER = "NotificationListener"; + + /** The Constant NOTIFICATION_DESERIALIZER. */ + private static final String NOTIFICATION_DESERIALIZER = "NotificationDeserializer"; /** The Constant USER_VERIFIER_CONSTANTS. */ private static final String USER_VERIFIER_CONSTANTS = "UserVerifierConstants"; @@ -257,14 +263,26 @@ public Sdk generateSdk(String buildVersion, List bootstrapNod javaSources.add(profileSerializerClassBean); Schema notificationSchema = new Schema.Parser().parse(notificationSchemaBody); + String notificationClassName = profileSchema.getName(); + String notificationClassPackage = profileSchema.getNamespace(); + javaSources.addAll(generateSchemaSources(notificationSchema)); - String notificationListenerTemplate = readResource(ABSTRACT_NOTIFICATION_LISTENER_SOURCE_TEMPLATE); + String notificationListenerTemplate = readResource(NOTIFICATION_LISTENER_SOURCE_TEMPLATE); String notificationListenerSource = notificationListenerTemplate.replaceAll(NOTIFICATION_CLASS_PACKAGE_VAR, - notificationSchema.getNamespace()).replaceAll(NOTIFICATION_CLASS_VAR, notificationSchema.getName()); + notificationClassPackage).replaceAll(NOTIFICATION_CLASS_VAR, notificationClassName); - JavaDynamicBean notificationListenerClassBean = new JavaDynamicBean(ABSTRACT_NOTIFICATION_LISTENER, notificationListenerSource); + JavaDynamicBean notificationListenerClassBean = new JavaDynamicBean(NOTIFICATION_LISTENER, notificationListenerSource); javaSources.add(notificationListenerClassBean); + javaSources.addAll(generateSchemaSources(notificationSchema)); + String notificationDeserializerSourceTemplate = readResource(NOTIFICATION_DESERIALIZER_SOURCE_TEMPLATE); + String notificationDeserializerSource = notificationDeserializerSourceTemplate.replaceAll(NOTIFICATION_CLASS_PACKAGE_VAR, + notificationClassPackage).replaceAll(NOTIFICATION_CLASS_VAR, notificationClassName); + + JavaDynamicBean notificationDeserializerClassBean = new JavaDynamicBean(NOTIFICATION_LISTENER, notificationDeserializerSource); + javaSources.add(notificationDeserializerClassBean); + + Schema logSchema = new Schema.Parser().parse(logSchemaBody); javaSources.addAll(generateSchemaSources(logSchema)); String logRecordTemplate = readResource(LOG_RECORD_SOURCE_TEMPLATE); diff --git a/server/control/src/main/resources/sdk/java/AbstractNotificationListener.java.template b/server/control/src/main/resources/sdk/java/AbstractNotificationListener.java.template deleted file mode 100644 index 5ef4e168f6..0000000000 --- a/server/control/src/main/resources/sdk/java/AbstractNotificationListener.java.template +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Copyright 2014 CyberVision, Inc. - * - * 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 org.kaaproject.kaa.client.notification; - -import java.io.IOException; -import java.nio.ByteBuffer; - -import org.apache.avro.specific.SpecificRecordBase; -import org.kaaproject.kaa.common.avro.AvroByteArrayConverter; -import ${notification_class_package}.${notification_class}; - -public abstract class AbstractNotificationListener implements NotificationListener { - private final AvroByteArrayConverter<${notification_class}> converter; - - public AbstractNotificationListener() { - converter = new AvroByteArrayConverter<${notification_class}>(${notification_class}.class); - } - - @Override - public final void onNotificationRaw(String topicName, ByteBuffer notification) throws IOException { - onNotification(topicName, converter.fromByteArray(notification.array())); - } - - public abstract void onNotification(String topicName, ${notification_class} notification); - -} diff --git a/server/control/src/main/resources/sdk/java/AbstractProfileContainer.java.template b/server/control/src/main/resources/sdk/java/AbstractProfileContainer.java.template deleted file mode 100644 index 20a674e279..0000000000 --- a/server/control/src/main/resources/sdk/java/AbstractProfileContainer.java.template +++ /dev/null @@ -1,84 +0,0 @@ -/* - * Copyright 2014 CyberVision, Inc. - * - * 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 org.kaaproject.kaa.client.profile; - -import java.io.IOException; - -import org.apache.avro.specific.SpecificRecordBase; -import org.kaaproject.kaa.common.avro.AvroByteArrayConverter; -import ${profile_class_package}.${profile_class}; - -/** - * Container for the profile object which should be implemented by the user - * - * @author yzeygerman - * - */ -public abstract class AbstractProfileContainer implements ProfileContainer { - private ProfileListener listener; - private AvroByteArrayConverter<${profile_class}> converter; - - /** - * Constructor for the AbstractProfileContainer. - * - * @param listener listener of profile updates. - * @param profileClass class object of the user-defined profile. - * - */ - public AbstractProfileContainer() { - this.converter = new AvroByteArrayConverter<${profile_class}>(${profile_class}.class); - } - - /** - * Retrieves serialized profile. - * - * @return byte array with avro serialized profile. - * - */ - public byte [] getSerializedProfile() throws IOException { - return converter.toByteArray(getProfile()); - } - - /** - * Updates profile. Call this method when you finished updating your profile. - * - */ - protected final void updateProfile() throws IOException { - if (listener != null) { - listener.onProfileUpdated(getSerializedProfile()); - } - } - - /** - * Sets new profile listener. - * - * @param listener new listener. - * - */ - public final void setProfileListener(ProfileListener listener) { - this.listener = listener; - } - - /** - * Retrieves user-defined profile object. Should be implemented by the user. - * - * @return profile object - * - */ - public abstract ${profile_class} getProfile(); - -} diff --git a/server/control/src/main/resources/sdk/java/nf/NotificationDeserializer.java.template b/server/control/src/main/resources/sdk/java/nf/NotificationDeserializer.java.template new file mode 100644 index 0000000000..e6a3a1ed62 --- /dev/null +++ b/server/control/src/main/resources/sdk/java/nf/NotificationDeserializer.java.template @@ -0,0 +1,23 @@ +package org.kaaproject.kaa.client.notification; + +import java.io.IOException; +import org.kaaproject.kaa.common.avro.AvroByteArrayConverter; +import ${notification_class_package}.${notification_class}; + +/** + * This class deserialize binary data to notification object. + * + * This implementation is auto-generated. Please modify corresponding template file. + * + * @author Andrew Shvayka + * + */ +public class NotificationDeserializer { + + private final AvroByteArrayConverter<${notification_class}> converter = new AvroByteArrayConverter<${notification_class}>(${notification_class}.class); + + ${notification_class} fromByteArray(byte[] data) throws IOException{ + return converter.fromByteArray(data); + } + +} diff --git a/server/control/src/main/resources/sdk/java/nf/NotificationListener.java.template b/server/control/src/main/resources/sdk/java/nf/NotificationListener.java.template new file mode 100644 index 0000000000..d692e31843 --- /dev/null +++ b/server/control/src/main/resources/sdk/java/nf/NotificationListener.java.template @@ -0,0 +1,38 @@ +/* + * Copyright 2014 CyberVision, Inc. + * + * 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 org.kaaproject.kaa.client.notification; + +import ${notification_class_package}.${notification_class}; + +/** + * The listener to notifications. + * + * @author Andrew Shvayka + * + */ +public interface NotificationListener { + + /** + * Called on each new notification. + * + * @param topicId the topic's id. + * @param notification the notification object. + * + */ + void onNotification(String topicId, ${notification_class} notification); + +}