Skip to content

Commit

Permalink
KAA-343: Updated java sdk generation and added profile serializer tem…
Browse files Browse the repository at this point in the history
…plate.
  • Loading branch information
Igor Khanenko committed Feb 10, 2015
1 parent 26e49ef commit 742a8c1
Show file tree
Hide file tree
Showing 5 changed files with 168 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,26 @@

/**
* This class serialize entity defined in profile schema and returned by profile container.
* This class have a special behavior in case of default schema and serialize default profile disregarding empty profile container.
*
* This class have a special behavior in case of default schema and serialize default profile
* disregarding empty profile container.
* <p/>
* This implementation is auto-generated. Please modify corresponding template file.
*
* @author Andrew Shvayka
*
* @author Andrew Shvayka
*/
class ProfileSerializer {
private final AvroByteArrayConverter<Profile> converter = new AvroByteArrayConverter<Profile>(Profile.class);

byte[] toByteArray(ProfileContainer container) throws IOException {
if(container == null){
Profile profile;
if (container == null) {
throw new RuntimeException("Profile container is not set!");
} else {
profile = container.getProfile();
}
Profile profile = container.getProfile();
if(profile != null){
if (profile != null) {
return converter.toByteArray(profile);
}else{
} else {
throw new RuntimeException("Profile is not set!");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,14 @@ 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 ABSTRACT_RPOFILE_CONTAINER_SOURCE_TEMPLATE. */
private static final String ABSTRACT_RPOFILE_CONTAINER_SOURCE_TEMPLATE = "sdk/java/AbstractProfileContainer.java.template";
/** The Constant PROFILE_CONTAINER_SOURCE_TEMPLATE. */
private static final String PROFILE_CONTAINER_SOURCE_TEMPLATE = "sdk/java/profile/ProfileContainer.java.template";

/** The Constant PROFILE_SERIALIZER_SOURCE_TEMPLATE. */
private static final String PROFILE_SERIALIZER_SOURCE_TEMPLATE = "sdk/java/profile/ProfileSerializer.java.template";

/** 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";
Expand All @@ -139,7 +145,10 @@ public class JavaSdkGenerator extends SdkGenerator {
private static final String USER_VERIFIER_CONSTANTS_SOURCE_TEMPLATE = "sdk/java/event/UserVerifierConstants.java.template";

/** The Constant ABSTRACT_PROFILE_CONTAINER. */
private static final String ABSTRACT_PROFILE_CONTAINER = "AbstractProfileContainer";
private static final String PROFILE_CONTAINER = "ProfileContainer";

/** 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";
Expand Down Expand Up @@ -226,15 +235,27 @@ public Sdk generateSdk(String buildVersion, List<BootstrapNodeInfo> bootstrapNod
replacementData.put(CLIENT_PROPERTIES, new ZipEntryData(new ZipEntry(CLIENT_PROPERTIES), clientPropertiesData));

Schema profileSchema = new Schema.Parser().parse(profileSchemaBody);
String profileClassName = profileSchema.getName();
String profileClassPackage = profileSchema.getNamespace();

List<JavaDynamicBean> javaSources = generateSchemaSources(profileSchema);
String profileContainerTemplate = readResource(ABSTRACT_RPOFILE_CONTAINER_SOURCE_TEMPLATE);
String profileContainerSource = profileContainerTemplate.replaceAll(PROFILE_CLASS_PACKAGE_VAR, profileSchema.getNamespace())
.replaceAll(PROFILE_CLASS_VAR, profileSchema.getName());

JavaDynamicBean profileContainerClassBean = new JavaDynamicBean(ABSTRACT_PROFILE_CONTAINER, profileContainerSource);
String profileContainerTemplate = readResource(PROFILE_CONTAINER_SOURCE_TEMPLATE);
String profileContainerSource = profileContainerTemplate.replaceAll(PROFILE_CLASS_PACKAGE_VAR, profileClassPackage)
.replaceAll(PROFILE_CLASS_VAR, profileClassName);
JavaDynamicBean profileContainerClassBean = new JavaDynamicBean(PROFILE_CONTAINER, profileContainerSource);
javaSources.add(profileContainerClassBean);

String profileSerializerTemplate;
if (profileSchemaVersion == 1) {
profileSerializerTemplate = readResource(DEFAULT_PROFILE_SERIALIZER_SOURCE_TEMPLATE);
} else {
profileSerializerTemplate = readResource(PROFILE_SERIALIZER_SOURCE_TEMPLATE);
}
String profileSerializerSource = profileSerializerTemplate.replaceAll(PROFILE_CLASS_PACKAGE_VAR, profileClassPackage)
.replaceAll(PROFILE_CLASS_VAR, profileClassName);
JavaDynamicBean profileSerializerClassBean = new JavaDynamicBean(PROFILE_SERIALIZER, profileSerializerSource);
javaSources.add(profileSerializerClassBean);

Schema notificationSchema = new Schema.Parser().parse(notificationSchemaBody);
javaSources.addAll(generateSchemaSources(notificationSchema));
String notificationListenerTemplate = readResource(ABSTRACT_NOTIFICATION_LISTENER_SOURCE_TEMPLATE);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* 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.kaaproject.kaa.common.avro.AvroByteArrayConverter;
import ${profile_class_package}.${profile_class};

/**
* This class serialize entity defined in profile schema and returned by profile container.
* This class have a special behavior in case of default schema and serialize default profile
* disregarding empty profile container.
* <p/>
* This implementation is auto-generated. Please modify corresponding template file.
*
* @author Andrew Shvayka
*/
class ProfileSerializer {

private final AvroByteArrayConverter<${profile_class}> converter = new AvroByteArrayConverter<${profile_class}>(${profile_class}.class);

byte[] toByteArray(ProfileContainer container) throws IOException {
${profile_class} profile;
if (container != null) {
profile = container.getProfile();
}
if (profile == null) {
profile = new ${profile_class}();
}
return converter.toByteArray(profile);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* 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 ${profile_class_package}.${profile_class};

/**
* Interface for the profile container.
*/
public interface ProfileContainer {

/**
* Retrieves serialized profile
*
* @return byte array with serialized profile
*
*/
${profile_class} getProfile();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* 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.kaaproject.kaa.common.avro.AvroByteArrayConverter;
import ${profile_class_package}.${profile_class};

/**
* This class serialize entity defined in profile schema and returned by profile container.
* This class have a special behavior in case of default schema and serialize default profile
* disregarding empty profile container.
* <p/>
* This implementation is auto-generated. Please modify corresponding template file.
*
* @author Andrew Shvayka
*/
class ProfileSerializer {

private final AvroByteArrayConverter<${profile_class}> converter = new AvroByteArrayConverter<${profile_class}>(${profile_class}.class);

byte[] toByteArray(ProfileContainer container) throws IOException {
${profile_class} profile;
if (container == null) {
throw new RuntimeException("Profile container is not set!");
} else {
profile = container.getProfile();
}
if (profile != null) {
return converter.toByteArray(profile);
} else {
throw new RuntimeException("Profile is not set!");
}
}
}

0 comments on commit 742a8c1

Please sign in to comment.