Adding application instance event, processor and listner
Project: http://git-wip-us.apache.org/repos/asf/stratos/repo Commit: http://git-wip-us.apache.org/repos/asf/stratos/commit/608b368e Tree: http://git-wip-us.apache.org/repos/asf/stratos/tree/608b368e Diff: http://git-wip-us.apache.org/repos/asf/stratos/diff/608b368e Branch: refs/heads/master Commit: 608b368ea99f29254b4064435b63cf4a00d87b40 Parents: efd079d Author: Lahiru Sandaruwan <[email protected]> Authored: Thu Dec 4 22:25:20 2014 +0530 Committer: Lahiru Sandaruwan <[email protected]> Committed: Thu Dec 4 22:30:18 2014 +0530 ---------------------------------------------------------------------- .../ApplicationInstanceCreatedEvent.java | 50 +++++++++ ...ApplicationInstanceCreatedEventListener.java | 27 +++++ ...licationInstanceCreatedMessageProcessor.java | 110 +++++++++++++++++++ 3 files changed, 187 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/stratos/blob/608b368e/components/org.apache.stratos.messaging/src/main/java/org/apache/stratos/messaging/event/applications/ApplicationInstanceCreatedEvent.java ---------------------------------------------------------------------- diff --git a/components/org.apache.stratos.messaging/src/main/java/org/apache/stratos/messaging/event/applications/ApplicationInstanceCreatedEvent.java b/components/org.apache.stratos.messaging/src/main/java/org/apache/stratos/messaging/event/applications/ApplicationInstanceCreatedEvent.java new file mode 100644 index 0000000..5f517da --- /dev/null +++ b/components/org.apache.stratos.messaging/src/main/java/org/apache/stratos/messaging/event/applications/ApplicationInstanceCreatedEvent.java @@ -0,0 +1,50 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.stratos.messaging.event.applications; + +import org.apache.stratos.messaging.domain.applications.Application; +import org.apache.stratos.messaging.domain.instance.ApplicationInstance; +import org.apache.stratos.messaging.event.Event; + +import java.io.Serializable; + +/** + * This event will be fired upon the application created is detected. + */ +public class ApplicationInstanceCreatedEvent extends Event implements Serializable { + private static final long serialVersionUID = 2625412714611885089L; + + private String applicationId; + + private ApplicationInstance applicationInstance; + + public ApplicationInstanceCreatedEvent(String applicationId, ApplicationInstance applicationInstance) { + + this.applicationId = applicationId; + this.applicationInstance = applicationInstance; + } + + public String getApplicationId() { + return applicationId; + } + + public ApplicationInstance getApplicationInstance() { + return applicationInstance; + } +} http://git-wip-us.apache.org/repos/asf/stratos/blob/608b368e/components/org.apache.stratos.messaging/src/main/java/org/apache/stratos/messaging/listener/applications/ApplicationInstanceCreatedEventListener.java ---------------------------------------------------------------------- diff --git a/components/org.apache.stratos.messaging/src/main/java/org/apache/stratos/messaging/listener/applications/ApplicationInstanceCreatedEventListener.java b/components/org.apache.stratos.messaging/src/main/java/org/apache/stratos/messaging/listener/applications/ApplicationInstanceCreatedEventListener.java new file mode 100644 index 0000000..8ebe2ce --- /dev/null +++ b/components/org.apache.stratos.messaging/src/main/java/org/apache/stratos/messaging/listener/applications/ApplicationInstanceCreatedEventListener.java @@ -0,0 +1,27 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.stratos.messaging.listener.applications; + +import org.apache.stratos.messaging.listener.EventListener; + +/** + * This listener will get triggered upon the application instance created event. + */ +public abstract class ApplicationInstanceCreatedEventListener extends EventListener { +} http://git-wip-us.apache.org/repos/asf/stratos/blob/608b368e/components/org.apache.stratos.messaging/src/main/java/org/apache/stratos/messaging/message/processor/applications/ApplicationInstanceCreatedMessageProcessor.java ---------------------------------------------------------------------- diff --git a/components/org.apache.stratos.messaging/src/main/java/org/apache/stratos/messaging/message/processor/applications/ApplicationInstanceCreatedMessageProcessor.java b/components/org.apache.stratos.messaging/src/main/java/org/apache/stratos/messaging/message/processor/applications/ApplicationInstanceCreatedMessageProcessor.java new file mode 100644 index 0000000..e92d581 --- /dev/null +++ b/components/org.apache.stratos.messaging/src/main/java/org/apache/stratos/messaging/message/processor/applications/ApplicationInstanceCreatedMessageProcessor.java @@ -0,0 +1,110 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.stratos.messaging.message.processor.applications; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.stratos.messaging.domain.applications.Applications; +import org.apache.stratos.messaging.domain.instance.ApplicationInstance; +import org.apache.stratos.messaging.event.applications.ApplicationInstanceCreatedEvent; +import org.apache.stratos.messaging.message.processor.MessageProcessor; +import org.apache.stratos.messaging.message.processor.applications.updater.ApplicationsUpdater; +import org.apache.stratos.messaging.util.Util; + +public class ApplicationInstanceCreatedMessageProcessor extends MessageProcessor { + + private static final Log log = LogFactory.getLog(ApplicationInstanceCreatedMessageProcessor.class); + private MessageProcessor nextProcessor; + + @Override + public void setNext(MessageProcessor nextProcessor) { + this.nextProcessor = nextProcessor; + } + + @Override + public boolean process(String type, String message, Object object) { + + Applications applications = (Applications) object; + + if (ApplicationInstanceCreatedEvent.class.getName().equals(type)) { + + if (!applications.isInitialized()) { + return false; + } + + + ApplicationInstanceCreatedEvent event = (ApplicationInstanceCreatedEvent) Util.jsonToObject(message, + ApplicationInstanceCreatedEvent.class); + if (event == null) { + log.error("Unable to convert the JSON message to ApplicationInstanceCreatedEvent"); + return false; + } + + ApplicationsUpdater.acquireWriteLockForApplications(); + try { + return doProcess(event, applications); + + } finally { + ApplicationsUpdater.releaseWriteLockForApplications(); + } + + } else { + if (nextProcessor != null) { + // ask the next processor to take care of the message. + return nextProcessor.process(type, message, applications); + } else { + throw new RuntimeException(String.format("Failed to process message using available message processors: [type] %s [body] %s", type, message)); + } + } + } + + private boolean doProcess(ApplicationInstanceCreatedEvent event, Applications applications) { + + // check if required properties are available + if (event.getApplicationInstance() == null) { + String errorMsg = "Application instance object of application instance created event is invalid"; + log.error(errorMsg); + throw new RuntimeException(errorMsg); + + } + + ApplicationInstance applicationInstance = event.getApplicationInstance(); + + if (applicationInstance.getInstanceId() == null || applicationInstance.getInstanceId().isEmpty()) { + String errorMsg = "App instance id of application instance created event is invalid: [ " + + applicationInstance.getInstanceId() + " ]"; + log.error(errorMsg); + throw new RuntimeException(errorMsg); + } + + // check if an Application instance with same name exists in applications instance + if (null != applications.getApplication(event.getApplicationId()).getInstanceByNetworkPartitionId(applicationInstance.getNetworkPartitionId())) { + + log.warn("Application instance with id [ " + applicationInstance.getInstanceId() + " ] already exists"); + + } else { + // add application instance to Application Topology + applications.getApplication(event.getApplicationId()).addInstance(applicationInstance.getInstanceId(), applicationInstance); + } + + notifyEventListeners(event); + return true; + } +}
