summaryrefslogtreecommitdiffstats
path: root/wso2/dropwizard-ioc-container
diff options
context:
space:
mode:
authorZhaoxing <meng.zhaoxing1@zte.com.cn>2017-08-08 14:02:53 +0800
committerZhaoxing <meng.zhaoxing1@zte.com.cn>2017-08-08 14:02:53 +0800
commitc0604184b2aa8cff924ca783ec6b36f1f5988775 (patch)
tree1c70ffe1ad90c915b3382a37eb2fa80901519c7b /wso2/dropwizard-ioc-container
parent706fc9fed496972968fde136c3e4e10f9578a5b3 (diff)
init code
Change-Id: Icd0948118397b256da70dfbcbbec5520dc5eafd4 Signed-off-by: Zhaoxing <meng.zhaoxing1@zte.com.cn>
Diffstat (limited to 'wso2/dropwizard-ioc-container')
-rw-r--r--wso2/dropwizard-ioc-container/pom.xml80
-rw-r--r--wso2/dropwizard-ioc-container/src/main/java/org/openo/dropwizard/ioc/annotation/BaseService.java34
-rw-r--r--wso2/dropwizard-ioc-container/src/main/java/org/openo/dropwizard/ioc/annotation/Lazy.java34
-rw-r--r--wso2/dropwizard-ioc-container/src/main/java/org/openo/dropwizard/ioc/annotation/PostBaseService.java34
-rw-r--r--wso2/dropwizard-ioc-container/src/main/java/org/openo/dropwizard/ioc/annotation/PreBaseService.java34
-rw-r--r--wso2/dropwizard-ioc-container/src/main/java/org/openo/dropwizard/ioc/annotation/PreLoad.java34
-rw-r--r--wso2/dropwizard-ioc-container/src/main/java/org/openo/dropwizard/ioc/annotation/PreServiceLoad.java35
-rw-r--r--wso2/dropwizard-ioc-container/src/main/java/org/openo/dropwizard/ioc/bundle/AutoConfigBundle.java430
-rw-r--r--wso2/dropwizard-ioc-container/src/main/java/org/openo/dropwizard/ioc/bundle/AutoConfigBundleBuider.java42
-rw-r--r--wso2/dropwizard-ioc-container/src/main/java/org/openo/dropwizard/ioc/bundle/IOCApplication.java42
-rw-r--r--wso2/dropwizard-ioc-container/src/main/java/org/openo/dropwizard/ioc/bundle/ServiceLocatorManaged.java51
-rw-r--r--wso2/dropwizard-ioc-container/src/main/java/org/openo/dropwizard/ioc/utils/ServiceBinder.java52
-rw-r--r--wso2/dropwizard-ioc-container/src/main/java/org/openo/dropwizard/ioc/utils/ServiceLocatorHolder.java36
-rw-r--r--wso2/dropwizard-ioc-container/src/test/java/org/openo/dropwizard/ioc/test/service/IOCBundleTest.java49
-rw-r--r--wso2/dropwizard-ioc-container/src/test/java/org/openo/dropwizard/ioc/test/service/PreTestService.java33
-rw-r--r--wso2/dropwizard-ioc-container/src/test/java/org/openo/dropwizard/ioc/test/service/TestService.java41
-rw-r--r--wso2/dropwizard-ioc-container/src/test/java/org/openo/dropwizard/ioc/test/service/app/ExampleApp.java35
-rw-r--r--wso2/dropwizard-ioc-container/src/test/java/org/openo/dropwizard/ioc/test/service/conf/TestConfiguration.java26
-rw-r--r--wso2/dropwizard-ioc-container/src/test/resources/example.yml8
19 files changed, 1130 insertions, 0 deletions
diff --git a/wso2/dropwizard-ioc-container/pom.xml b/wso2/dropwizard-ioc-container/pom.xml
new file mode 100644
index 0000000..6223c7f
--- /dev/null
+++ b/wso2/dropwizard-ioc-container/pom.xml
@@ -0,0 +1,80 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!-- Copyright 2017 ZTE Corporation. 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. -->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+ <parent>
+ <groupId>org.openo.common-services.common-utilities</groupId>
+ <artifactId>common-setting</artifactId>
+ <version>1.1.0-SNAPSHOT</version>
+ </parent>
+ <artifactId>dropwizard-ioc-container</artifactId>
+ <name>common-services-common-utilities/dropwizard-ioc-container</name>
+ <properties>
+ </properties>
+ <dependencies>
+
+ <dependency>
+ <groupId>io.dropwizard</groupId>
+ <artifactId>dropwizard-core</artifactId>
+ <version>0.8.0</version>
+ <scope>provided</scope>
+ </dependency>
+
+ <dependency>
+ <groupId>org.reflections</groupId>
+ <artifactId>reflections</artifactId>
+ <version>0.9.10</version>
+ <exclusions>
+ <exclusion>
+ <groupId>com.google.guava</groupId>
+ <artifactId>guava</artifactId>
+ </exclusion>
+ </exclusions>
+ </dependency>
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <scope>test</scope>
+ <version>4.8.2</version>
+ </dependency>
+
+ </dependencies>
+
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-surefire-plugin</artifactId>
+ <configuration>
+ <forkMode>always</forkMode>
+ <argLine>-XX:-UseSplitVerifier</argLine>
+ <skip>${maven.test.skip}</skip>
+ <testFailureIgnore>${maven.test.failure.ignore}</testFailureIgnore>
+ <excludes>
+ <exclude>${excludesFile}</exclude>
+ </excludes>
+ </configuration>
+ </plugin>
+
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-compiler-plugin</artifactId>
+ <version>3.5</version>
+ <configuration>
+ <source>1.8</source>
+ <target>1.8</target>
+ <testSource>1.8</testSource>
+ <testTarget>1.8</testTarget>
+ </configuration>
+ </plugin>
+
+
+ </plugins>
+ </build>
+</project>
diff --git a/wso2/dropwizard-ioc-container/src/main/java/org/openo/dropwizard/ioc/annotation/BaseService.java b/wso2/dropwizard-ioc-container/src/main/java/org/openo/dropwizard/ioc/annotation/BaseService.java
new file mode 100644
index 0000000..ac0bb47
--- /dev/null
+++ b/wso2/dropwizard-ioc-container/src/main/java/org/openo/dropwizard/ioc/annotation/BaseService.java
@@ -0,0 +1,34 @@
+/**
+ * Copyright 2017 ZTE Corporation.
+ *
+ * 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.openo.dropwizard.ioc.annotation;
+
+import java.lang.annotation.Documented;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * basic services for identification
+ * @author hu.rui
+ *
+ */
+@Target({ElementType.TYPE})
+@Retention(RetentionPolicy.RUNTIME)
+@Documented
+public @interface BaseService {
+
+}
diff --git a/wso2/dropwizard-ioc-container/src/main/java/org/openo/dropwizard/ioc/annotation/Lazy.java b/wso2/dropwizard-ioc-container/src/main/java/org/openo/dropwizard/ioc/annotation/Lazy.java
new file mode 100644
index 0000000..40ce5af
--- /dev/null
+++ b/wso2/dropwizard-ioc-container/src/main/java/org/openo/dropwizard/ioc/annotation/Lazy.java
@@ -0,0 +1,34 @@
+/**
+ * Copyright 2017 ZTE Corporation.
+ *
+ * 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.openo.dropwizard.ioc.annotation;
+
+import java.lang.annotation.Documented;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * lazy loading of related services
+ * @author hu.rui
+ *
+ */
+@Target({ElementType.TYPE})
+@Retention(RetentionPolicy.RUNTIME)
+@Documented
+public @interface Lazy {
+
+}
diff --git a/wso2/dropwizard-ioc-container/src/main/java/org/openo/dropwizard/ioc/annotation/PostBaseService.java b/wso2/dropwizard-ioc-container/src/main/java/org/openo/dropwizard/ioc/annotation/PostBaseService.java
new file mode 100644
index 0000000..a1a6d77
--- /dev/null
+++ b/wso2/dropwizard-ioc-container/src/main/java/org/openo/dropwizard/ioc/annotation/PostBaseService.java
@@ -0,0 +1,34 @@
+/**
+ * Copyright 2017 ZTE Corporation.
+ *
+ * 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.openo.dropwizard.ioc.annotation;
+
+import java.lang.annotation.Documented;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * load on after basic services
+ * @author hu.rui
+ *
+ */
+@Target({ElementType.TYPE})
+@Retention(RetentionPolicy.RUNTIME)
+@Documented
+public @interface PostBaseService {
+
+}
diff --git a/wso2/dropwizard-ioc-container/src/main/java/org/openo/dropwizard/ioc/annotation/PreBaseService.java b/wso2/dropwizard-ioc-container/src/main/java/org/openo/dropwizard/ioc/annotation/PreBaseService.java
new file mode 100644
index 0000000..f579b9e
--- /dev/null
+++ b/wso2/dropwizard-ioc-container/src/main/java/org/openo/dropwizard/ioc/annotation/PreBaseService.java
@@ -0,0 +1,34 @@
+/**
+ * Copyright 2017 ZTE Corporation.
+ *
+ * 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.openo.dropwizard.ioc.annotation;
+
+import java.lang.annotation.Documented;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * prior to basic service loading
+ * @author hu.rui
+ *
+ */
+@Target({ElementType.TYPE})
+@Retention(RetentionPolicy.RUNTIME)
+@Documented
+public @interface PreBaseService {
+
+}
diff --git a/wso2/dropwizard-ioc-container/src/main/java/org/openo/dropwizard/ioc/annotation/PreLoad.java b/wso2/dropwizard-ioc-container/src/main/java/org/openo/dropwizard/ioc/annotation/PreLoad.java
new file mode 100644
index 0000000..3131e8b
--- /dev/null
+++ b/wso2/dropwizard-ioc-container/src/main/java/org/openo/dropwizard/ioc/annotation/PreLoad.java
@@ -0,0 +1,34 @@
+/**
+ * Copyright 2017 ZTE Corporation.
+ *
+ * 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.openo.dropwizard.ioc.annotation;
+
+import java.lang.annotation.Documented;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * load before general service
+ * @author hu.rui
+ *
+ */
+@Target({ElementType.TYPE})
+@Retention(RetentionPolicy.RUNTIME)
+@Documented
+public @interface PreLoad {
+
+}
diff --git a/wso2/dropwizard-ioc-container/src/main/java/org/openo/dropwizard/ioc/annotation/PreServiceLoad.java b/wso2/dropwizard-ioc-container/src/main/java/org/openo/dropwizard/ioc/annotation/PreServiceLoad.java
new file mode 100644
index 0000000..e0e3043
--- /dev/null
+++ b/wso2/dropwizard-ioc-container/src/main/java/org/openo/dropwizard/ioc/annotation/PreServiceLoad.java
@@ -0,0 +1,35 @@
+/**
+ * Copyright 2017 ZTE Corporation.
+ *
+ * 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.openo.dropwizard.ioc.annotation;
+
+import java.lang.annotation.Documented;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ *
+ * load before general service,after PreLoad
+ * @author hu.rui
+ *
+ */
+@Target({ElementType.TYPE})
+@Retention(RetentionPolicy.RUNTIME)
+@Documented
+public @interface PreServiceLoad {
+
+}
diff --git a/wso2/dropwizard-ioc-container/src/main/java/org/openo/dropwizard/ioc/bundle/AutoConfigBundle.java b/wso2/dropwizard-ioc-container/src/main/java/org/openo/dropwizard/ioc/bundle/AutoConfigBundle.java
new file mode 100644
index 0000000..4cb4008
--- /dev/null
+++ b/wso2/dropwizard-ioc-container/src/main/java/org/openo/dropwizard/ioc/bundle/AutoConfigBundle.java
@@ -0,0 +1,430 @@
+/**
+ * Copyright 2017 ZTE Corporation.
+ *
+ * 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.openo.dropwizard.ioc.bundle;
+
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Field;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+import javax.ws.rs.Path;
+import javax.ws.rs.ext.Provider;
+
+import org.eclipse.jetty.util.component.LifeCycle;
+import org.glassfish.hk2.api.ServiceLocator;
+import org.glassfish.hk2.api.ServiceLocatorFactory;
+import org.glassfish.hk2.utilities.ServiceLocatorUtilities;
+import org.glassfish.hk2.utilities.binding.AbstractBinder;
+import org.glassfish.jersey.servlet.ServletProperties;
+import org.jvnet.hk2.annotations.Service;
+import org.openo.dropwizard.ioc.annotation.BaseService;
+import org.openo.dropwizard.ioc.annotation.Lazy;
+import org.openo.dropwizard.ioc.annotation.PostBaseService;
+import org.openo.dropwizard.ioc.annotation.PreBaseService;
+import org.openo.dropwizard.ioc.annotation.PreLoad;
+import org.openo.dropwizard.ioc.annotation.PreServiceLoad;
+import org.openo.dropwizard.ioc.utils.ServiceBinder;
+import org.openo.dropwizard.ioc.utils.ServiceLocatorHolder;
+import org.reflections.Reflections;
+import org.reflections.scanners.SubTypesScanner;
+import org.reflections.scanners.TypeAnnotationsScanner;
+import org.reflections.util.ClasspathHelper;
+import org.reflections.util.ConfigurationBuilder;
+import org.reflections.util.FilterBuilder;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.codahale.metrics.health.HealthCheck;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.google.common.collect.Lists;
+
+import io.dropwizard.Configuration;
+import io.dropwizard.ConfiguredBundle;
+import io.dropwizard.configuration.ConfigurationSourceProvider;
+import io.dropwizard.lifecycle.Managed;
+import io.dropwizard.lifecycle.ServerLifecycleListener;
+import io.dropwizard.servlets.tasks.Task;
+import io.dropwizard.setup.Bootstrap;
+import io.dropwizard.setup.Environment;
+
+/**
+ * complete the integration of hK2 container and dropwizard
+ *
+ * @author hu.rui
+ *
+ */
+
+public class AutoConfigBundle<T extends Configuration> implements ConfiguredBundle<T> {
+
+ private static final Logger LOG = LoggerFactory.getLogger(AutoConfigBundle.class);
+
+ private ServiceLocator locator;
+ private Reflections reflections;
+ private Set<Class<?>> services;
+
+ private Bootstrap<?> bootstrap;
+
+
+ AutoConfigBundle(final String packageName) {
+ this(Lists.newArrayList(packageName));
+ }
+
+ AutoConfigBundle(List<String> packageNames) {
+ FilterBuilder filterBuilder = new FilterBuilder();
+
+ packageNames.stream().forEach(packageName -> {
+ filterBuilder.include(FilterBuilder.prefix(packageName));
+ });
+ ConfigurationBuilder reflectionCfg = new ConfigurationBuilder();
+
+ packageNames.stream().forEach(packageName -> {
+ reflectionCfg.addUrls(ClasspathHelper.forPackage(packageName));
+ });
+
+ reflectionCfg.filterInputsBy(filterBuilder).setScanners(new SubTypesScanner(),
+ new TypeAnnotationsScanner());
+ reflections = new Reflections(reflectionCfg);
+
+ locator = ServiceLocatorFactory.getInstance().create("dw-hk2");
+
+ ServiceLocatorHolder.setLocator(locator);
+
+ }
+
+ public static <T extends Configuration> AutoConfigBundleBuider<T> newBuilder() {
+ return new AutoConfigBundleBuider<T>();
+ }
+
+ @Override
+ public void initialize(final Bootstrap<?> bootstrap) {
+
+ this.bootstrap = bootstrap;
+ registerPreLoadService();
+
+ LOG.debug("Intialzing auto config bundle.");
+ }
+
+ private void registerPreLoadService() {
+
+ registerService(PreLoad.class);
+
+ }
+
+
+ @Override
+ public void run(final T configuration, final Environment environment) throws Exception {
+
+ registerConfigurationProvider(configuration, environment);
+
+
+ registerEnvironment(environment);
+ registerObjectMapper(environment);
+
+ environment.getApplicationContext().getServletContext()
+ .setAttribute(ServletProperties.SERVICE_LOCATOR, locator);
+
+ registerService(PreBaseService.class);
+ registerService(BaseService.class);
+ registerService(PostBaseService.class);
+ this.registerService(PreServiceLoad.class);
+
+
+ registerServices();
+
+ // registerManaged(environment);
+ registerLifecycle(environment);
+ registerServerLifecycleListeners(environment);
+ registerJettyLifeCycleListener(environment);
+ registerTasks(environment);
+ registerHealthChecks(environment);
+ registerProviders(environment);
+ registerResources(environment);
+
+ environment.lifecycle().manage(new ServiceLocatorManaged(locator));
+
+ }
+
+
+
+ private void registerProviders(Environment environment) {
+ reflections.getSubTypesOf(Provider.class).stream().filter(services::contains)
+ .forEach(providerKlass -> {
+ try {
+ environment.jersey().register(locator.getService(providerKlass));
+ } catch (Exception e) {
+ LOG.warn("", e);
+ }
+
+ LOG.info("Registering Dropwizard Provider, class name : {}", providerKlass.getName());
+
+ });
+
+ }
+
+ private void registerTasks(Environment environment) {
+ reflections.getSubTypesOf(Task.class).stream().filter(services::contains).forEach(taskKlass -> {
+ try {
+ environment.admin().addTask(locator.getService(taskKlass));
+ } catch (Exception e) {
+ LOG.warn("", e);
+ }
+ LOG.info("Registering Dropwizard Task, class name : {}", taskKlass.getName());
+ });
+
+ }
+
+ private void registerJettyLifeCycleListener(Environment environment) {
+ reflections.getSubTypesOf(LifeCycle.Listener.class).stream().filter(services::contains)
+ .forEach(lifecycleListenerKlass -> {
+
+ try {
+ environment.lifecycle()
+ .addLifeCycleListener(locator.getService(lifecycleListenerKlass));
+ } catch (Exception e) {
+ LOG.warn("", e);
+ }
+ LOG.info("Registering Dropwizard lifecycleListener, class name : {}",
+ lifecycleListenerKlass.getName());
+ });
+
+ }
+
+ private void registerServerLifecycleListeners(Environment environment) {
+
+ reflections.getSubTypesOf(ServerLifecycleListener.class).stream().filter(services::contains)
+ .forEach(serverLifecycleListenerKlass -> {
+ try {
+ environment.lifecycle()
+ .addServerLifecycleListener(locator.getService(serverLifecycleListenerKlass));
+ } catch (Exception e) {
+ LOG.warn("", e);
+ }
+ LOG.info("Registering Dropwizard serverLifecycleListener, class name : {}",
+ serverLifecycleListenerKlass.getName());
+ });
+
+ }
+
+ private void registerLifecycle(Environment environment) {
+ reflections.getSubTypesOf(LifeCycle.class).stream().filter(services::contains)
+ .forEach(lifeCycleKlass -> {
+ try {
+ environment.lifecycle().manage(locator.getService(lifeCycleKlass));
+ } catch (Exception e) {
+ LOG.warn("", e);
+ }
+ LOG.info("Registering Dropwizard LifeCycle, class name : {}", lifeCycleKlass.getName());
+ });
+ }
+
+ /*
+ * private void registerManaged(Environment environment) {
+ *
+ * reflections.getSubTypesOf(Managed.class).stream().filter(services::contains)
+ * .forEach(managedKlass -> { try {
+ * environment.lifecycle().manage(locator.getService(managedKlass)); } catch (Exception e) {
+ * LOG.warn("", e); } LOG.info("Registering Dropwizard managed, class name : {}",
+ * managedKlass.getName()); });
+ *
+ * }
+ */
+
+ private void registerObjectMapper(Environment environment) {
+
+ final ObjectMapper objectMapper = environment.getObjectMapper();
+
+ ServiceLocatorUtilities.bind(locator, new AbstractBinder() {
+ @Override
+ protected void configure() {
+ bind(objectMapper).to(ObjectMapper.class);
+
+ LOG.info("Registering Dropwizard objectMapper, class name : {}",
+ objectMapper.getClass().getName());
+ }
+ });
+
+ }
+
+ private void registerEnvironment(final Environment environment) {
+
+ ServiceLocatorUtilities.bind(locator, new AbstractBinder() {
+ @Override
+ protected void configure() {
+ bind(environment).to(Environment.class);
+
+ LOG.info("Registering Dropwizard environment, class name : {}",
+ environment.getClass().getName());
+ }
+ });
+
+ }
+
+ private void registerConfigurationProvider(final T configuration, final Environment environment) {
+
+ ServiceLocatorUtilities.bind(locator, new AbstractBinder() {
+ @Override
+ protected void configure() {
+ bind(configuration);
+ LOG.info("Registering Dropwizard Configuration class name:{}",
+ configuration.getClass().getName());
+ if (configuration instanceof Configuration) {
+ bind((Configuration) configuration).to(Configuration.class);
+ LOG.info("Registering Dropwizard Configuration class name:{}",
+ Configuration.class.getName());
+ }
+
+ }
+ });
+
+ registerSubConfigure(configuration, environment);
+
+ }
+
+ private void registerSubConfigure(final T configuration, final Environment environment) {
+ final List<Field> subDeclaredFields =
+ Arrays.asList(configuration.getClass().getDeclaredFields());
+ List<Field> parentDeclaredFields = Arrays.asList(Configuration.class.getDeclaredFields());
+
+ List<Field> filtersubDeclaredFields = subDeclaredFields.stream()
+ .filter(subDeclaredField -> !subDeclaredField.getType().isPrimitive())
+ .filter(subDeclaredField -> !subDeclaredField.getType().equals(String.class))
+ .filter(subDeclaredField -> !parentDeclaredFields.contains(subDeclaredField))
+ .collect(Collectors.toList());
+
+ ServiceLocatorUtilities.bind(locator, new AbstractBinder() {
+ @Override
+ protected void configure() {
+ filtersubDeclaredFields.forEach(subField -> {
+ subField.setAccessible(true);
+ try {
+ Object subConfig = subField.get(configuration);
+ if (subConfig != null) {
+ bind(subConfig);
+ LOG.info("Registering Dropwizard Sub Configuration class name {}",
+ subConfig.getClass().getName());
+ }
+
+ } catch (Exception e) {
+ LOG.error("bind sub config:{} fail", subField);
+ }
+ });
+ }
+ });
+
+ }
+
+ private void registerServices() {
+ services = this.reflections.getTypesAnnotatedWith(Service.class, true);
+ if (!services.isEmpty()) {
+ ServiceLocatorUtilities.bind(locator, new ServiceBinder(services));
+
+ services.forEach(s -> {
+ LOG.info("Registering Dropwizard service, class name : {}", s.getName());
+ });
+
+ services.stream().filter(serviceClazz -> (serviceClazz.getAnnotation(Lazy.class) == null))
+ .peek(serviceClazz -> LOG.info("active service, class name : {}", serviceClazz.getName()))
+ .forEach(serviceClazz -> {
+ try {
+ long startTime = System.currentTimeMillis();
+ locator.getService(serviceClazz);
+ LOG.info("active service, class name : {},cost time:{}", serviceClazz.getName(),
+ (System.currentTimeMillis() - startTime));
+ } catch (Exception e) {
+ LOG.warn("", e);
+ }
+
+ });
+
+ } else {
+ LOG.warn("Registering Dropwizard service is empty");
+
+ }
+
+ }
+
+ private void registerResources(final Environment environment) {
+ reflections.getTypesAnnotatedWith(Path.class).stream().forEach(resourceClass -> {
+
+ LOG.info("begin Registering Dropwizard resource, class name : {}", resourceClass.getName());
+ try {
+ Object resourceObject = locator.getService(resourceClass);
+ if (resourceObject != null) {
+ environment.jersey().register(resourceObject);
+ LOG.info("Registering Dropwizard resource, class name : {}", resourceClass.getName());
+ } else {
+ LOG.warn(resourceClass.getName() + " not use Service annotation");
+ }
+ } catch (Exception e) {
+ LOG.error("", e);
+ }
+
+
+ });
+ }
+
+ private void registerHealthChecks(final Environment env) {
+
+ reflections.getSubTypesOf(HealthCheck.class).stream().filter(services::contains)
+ .forEach(healthCheckKlass -> {
+ try {
+ env.healthChecks().register(healthCheckKlass.getName(),
+ locator.getService(healthCheckKlass));
+ } catch (Exception e) {
+ LOG.warn("", e);
+ }
+ LOG.info("Registering Dropwizard healthCheck, class name : {}",
+ healthCheckKlass.getName());
+ });
+
+ }
+
+
+
+ private void registerService(Class<? extends Annotation> annotationClazz) {
+
+ Set<Class<?>> services = this.reflections.getTypesAnnotatedWith(annotationClazz, true);
+ if (!services.isEmpty()) {
+ ServiceLocatorUtilities.bind(locator, new ServiceBinder(services));
+
+ services.forEach(s -> {
+ LOG.info("{} Registering service, class name : {}", annotationClazz.getName(),
+ s.getName());
+ });
+
+ services.stream().filter(serviceClazz -> (serviceClazz.getAnnotation(Lazy.class) == null))
+ .peek(serviceClazz -> LOG.info("active service, class name : {}", serviceClazz.getName()))
+ .forEach(serviceClazz -> {
+ try {
+ long startTime = System.currentTimeMillis();
+ locator.getService(serviceClazz);
+ LOG.info("active service, class name : {},cost time:{}", serviceClazz.getName(),
+ (System.currentTimeMillis() - startTime));
+ } catch (Exception e) {
+ LOG.warn("", e);
+ }
+
+ });
+
+ } else {
+ LOG.warn("Registering {} service is empty", annotationClazz.getName());
+
+ }
+
+ }
+}
diff --git a/wso2/dropwizard-ioc-container/src/main/java/org/openo/dropwizard/ioc/bundle/AutoConfigBundleBuider.java b/wso2/dropwizard-ioc-container/src/main/java/org/openo/dropwizard/ioc/bundle/AutoConfigBundleBuider.java
new file mode 100644
index 0000000..5e2cbc0
--- /dev/null
+++ b/wso2/dropwizard-ioc-container/src/main/java/org/openo/dropwizard/ioc/bundle/AutoConfigBundleBuider.java
@@ -0,0 +1,42 @@
+/**
+ * Copyright 2017 ZTE Corporation.
+ *
+ * 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.openo.dropwizard.ioc.bundle;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import io.dropwizard.Configuration;
+
+public class AutoConfigBundleBuider<T extends Configuration> {
+
+ private static final String DEFAULT_PACKAGE_NAME="org.openo";
+
+ private List<String> packageNames=new ArrayList<>();
+
+ public AutoConfigBundleBuider(){
+ packageNames.add( DEFAULT_PACKAGE_NAME);
+ }
+
+ public AutoConfigBundleBuider<T> addPackageName(String packageName) {
+ this.packageNames.add(packageName);
+ return this;
+ }
+
+
+ public AutoConfigBundle<T> build() {
+ return new AutoConfigBundle<T>(packageNames);
+ }
+}
diff --git a/wso2/dropwizard-ioc-container/src/main/java/org/openo/dropwizard/ioc/bundle/IOCApplication.java b/wso2/dropwizard-ioc-container/src/main/java/org/openo/dropwizard/ioc/bundle/IOCApplication.java
new file mode 100644
index 0000000..9465b8e
--- /dev/null
+++ b/wso2/dropwizard-ioc-container/src/main/java/org/openo/dropwizard/ioc/bundle/IOCApplication.java
@@ -0,0 +1,42 @@
+/**
+ * Copyright 2017 ZTE Corporation.
+ *
+ * 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.openo.dropwizard.ioc.bundle;
+
+import io.dropwizard.Application;
+import io.dropwizard.Configuration;
+import io.dropwizard.setup.Bootstrap;
+import io.dropwizard.setup.Environment;
+
+/**
+ * complete IOC container startup
+ *
+ * @author hu.rui2
+ *
+ */
+public abstract class IOCApplication<T extends Configuration> extends Application<T> {
+
+
+ @Override
+ public void initialize(Bootstrap<T> bootstrap) {
+ super.initialize(bootstrap);
+ bootstrap.addBundle(new AutoConfigBundleBuider().build());
+ }
+
+ @Override
+ public void run(T configuration, Environment environment) throws Exception {
+
+ }
+}
diff --git a/wso2/dropwizard-ioc-container/src/main/java/org/openo/dropwizard/ioc/bundle/ServiceLocatorManaged.java b/wso2/dropwizard-ioc-container/src/main/java/org/openo/dropwizard/ioc/bundle/ServiceLocatorManaged.java
new file mode 100644
index 0000000..4d33021
--- /dev/null
+++ b/wso2/dropwizard-ioc-container/src/main/java/org/openo/dropwizard/ioc/bundle/ServiceLocatorManaged.java
@@ -0,0 +1,51 @@
+/**
+ * Copyright 2017 ZTE Corporation.
+ *
+ * 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.openo.dropwizard.ioc.bundle;
+
+import org.glassfish.hk2.api.ServiceLocator;
+
+import io.dropwizard.lifecycle.Managed;
+
+/**
+ * Life cycle management for IOC containers
+ * @author hu.rui
+ *
+ */
+public class ServiceLocatorManaged implements Managed{
+
+
+
+ private ServiceLocator locator;
+
+
+
+ public ServiceLocatorManaged(ServiceLocator locator) {
+ super();
+ this.locator = locator;
+ }
+
+ @Override
+ public void start() throws Exception {
+
+
+ }
+
+ @Override
+ public void stop() throws Exception {
+ locator.shutdown();
+ }
+
+}
diff --git a/wso2/dropwizard-ioc-container/src/main/java/org/openo/dropwizard/ioc/utils/ServiceBinder.java b/wso2/dropwizard-ioc-container/src/main/java/org/openo/dropwizard/ioc/utils/ServiceBinder.java
new file mode 100644
index 0000000..9ad984f
--- /dev/null
+++ b/wso2/dropwizard-ioc-container/src/main/java/org/openo/dropwizard/ioc/utils/ServiceBinder.java
@@ -0,0 +1,52 @@
+/**
+ * Copyright 2017 ZTE Corporation.
+ *
+ * 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.openo.dropwizard.ioc.utils;
+
+import java.util.Set;
+
+import org.glassfish.hk2.utilities.binding.AbstractBinder;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * @author hu.rui
+ *
+ */
+public class ServiceBinder extends AbstractBinder {
+
+ private static final Logger LOG = LoggerFactory.getLogger(ServiceBinder.class);
+
+ final Set<Class<?>> klasses;
+
+ public ServiceBinder(Set<Class<?>> services) {
+ this.klasses = services;
+ }
+
+ @Override
+ protected void configure() {
+ for (Class<?> klass : this.klasses) {
+
+ try{
+ LOG.info("start active class:"+klass.getName());
+ addActiveDescriptor(klass);
+ }catch(Exception e){
+ LOG.info("active class error:"+klass.getName(),e);
+ }
+
+
+ }
+ }
+}
diff --git a/wso2/dropwizard-ioc-container/src/main/java/org/openo/dropwizard/ioc/utils/ServiceLocatorHolder.java b/wso2/dropwizard-ioc-container/src/main/java/org/openo/dropwizard/ioc/utils/ServiceLocatorHolder.java
new file mode 100644
index 0000000..46dcd4b
--- /dev/null
+++ b/wso2/dropwizard-ioc-container/src/main/java/org/openo/dropwizard/ioc/utils/ServiceLocatorHolder.java
@@ -0,0 +1,36 @@
+/**
+ * Copyright 2017 ZTE Corporation.
+ *
+ * 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.openo.dropwizard.ioc.utils;
+
+import org.glassfish.hk2.api.ServiceLocator;
+
+/**
+ * @author hu.rui
+ *
+ */
+public class ServiceLocatorHolder {
+
+ private static ServiceLocator locator;
+
+ public static ServiceLocator getLocator() {
+ return locator;
+ }
+
+ public static void setLocator(ServiceLocator locator) {
+ ServiceLocatorHolder.locator = locator;
+ }
+
+}
diff --git a/wso2/dropwizard-ioc-container/src/test/java/org/openo/dropwizard/ioc/test/service/IOCBundleTest.java b/wso2/dropwizard-ioc-container/src/test/java/org/openo/dropwizard/ioc/test/service/IOCBundleTest.java
new file mode 100644
index 0000000..b0cac65
--- /dev/null
+++ b/wso2/dropwizard-ioc-container/src/test/java/org/openo/dropwizard/ioc/test/service/IOCBundleTest.java
@@ -0,0 +1,49 @@
+/**
+ * Copyright 2017 ZTE Corporation.
+ *
+ * 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.openo.dropwizard.ioc.test.service;
+
+import org.glassfish.hk2.api.ServiceLocator;
+import org.junit.Before;
+import org.junit.Test;
+import org.openo.dropwizard.ioc.test.service.app.ExampleApp;
+import org.openo.dropwizard.ioc.utils.ServiceLocatorHolder;
+
+import junit.framework.Assert;
+
+public class IOCBundleTest {
+
+
+
+ @Before
+ public void setUp() throws Exception {
+ ExampleApp.main(null);
+ }
+
+ @Test
+ public void test() {
+
+ ServiceLocator locator = ServiceLocatorHolder.getLocator();
+
+ TestService testService = locator.getService(TestService.class);
+
+ Assert.assertNotNull(testService);
+
+ Assert.assertEquals(TestService.HELLO, testService.sayHello());
+
+ Assert.assertEquals(PreTestService.HELLO, testService.sayHelloToPreTestService());
+ }
+
+}
diff --git a/wso2/dropwizard-ioc-container/src/test/java/org/openo/dropwizard/ioc/test/service/PreTestService.java b/wso2/dropwizard-ioc-container/src/test/java/org/openo/dropwizard/ioc/test/service/PreTestService.java
new file mode 100644
index 0000000..51cc386
--- /dev/null
+++ b/wso2/dropwizard-ioc-container/src/test/java/org/openo/dropwizard/ioc/test/service/PreTestService.java
@@ -0,0 +1,33 @@
+/**
+ * Copyright 2017 ZTE Corporation.
+ *
+ * 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.openo.dropwizard.ioc.test.service;
+
+import org.openo.dropwizard.ioc.annotation.PreServiceLoad;
+
+/**
+ * @author hu.rui
+ *
+ */
+@PreServiceLoad
+public class PreTestService {
+
+ public static final String HELLO = "hello Pre Service";
+
+ public String sayHello(){
+ return HELLO;
+ }
+
+}
diff --git a/wso2/dropwizard-ioc-container/src/test/java/org/openo/dropwizard/ioc/test/service/TestService.java b/wso2/dropwizard-ioc-container/src/test/java/org/openo/dropwizard/ioc/test/service/TestService.java
new file mode 100644
index 0000000..3d0f945
--- /dev/null
+++ b/wso2/dropwizard-ioc-container/src/test/java/org/openo/dropwizard/ioc/test/service/TestService.java
@@ -0,0 +1,41 @@
+/**
+ * Copyright 2017 ZTE Corporation.
+ *
+ * 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.openo.dropwizard.ioc.test.service;
+
+import javax.inject.Inject;
+
+import org.jvnet.hk2.annotations.Service;
+
+/**
+ * @author hu.rui
+ *
+ */
+@Service
+public class TestService {
+
+ @Inject
+ private PreTestService preTestService;
+
+ public static final String HELLO = "hello Service";
+
+ public String sayHello(){
+ return HELLO;
+ }
+
+ public String sayHelloToPreTestService(){
+ return preTestService.sayHello();
+ }
+}
diff --git a/wso2/dropwizard-ioc-container/src/test/java/org/openo/dropwizard/ioc/test/service/app/ExampleApp.java b/wso2/dropwizard-ioc-container/src/test/java/org/openo/dropwizard/ioc/test/service/app/ExampleApp.java
new file mode 100644
index 0000000..1e1f3ea
--- /dev/null
+++ b/wso2/dropwizard-ioc-container/src/test/java/org/openo/dropwizard/ioc/test/service/app/ExampleApp.java
@@ -0,0 +1,35 @@
+/**
+ * Copyright 2017 ZTE Corporation.
+ *
+ * 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.openo.dropwizard.ioc.test.service.app;
+
+import org.openo.dropwizard.ioc.bundle.IOCApplication;
+import org.openo.dropwizard.ioc.test.service.conf.TestConfiguration;
+
+public class ExampleApp extends IOCApplication<TestConfiguration>{
+
+ private static final String CONFIGURATION_FILE = "example.yml";
+
+ public static void main(String[] args) throws Exception {
+
+ String configFile = ExampleApp.class.getClassLoader().getResource(CONFIGURATION_FILE).getFile();
+
+ args = new String[]{"server",configFile};
+
+ new ExampleApp().run(args);
+ }
+
+
+}
diff --git a/wso2/dropwizard-ioc-container/src/test/java/org/openo/dropwizard/ioc/test/service/conf/TestConfiguration.java b/wso2/dropwizard-ioc-container/src/test/java/org/openo/dropwizard/ioc/test/service/conf/TestConfiguration.java
new file mode 100644
index 0000000..82c30cd
--- /dev/null
+++ b/wso2/dropwizard-ioc-container/src/test/java/org/openo/dropwizard/ioc/test/service/conf/TestConfiguration.java
@@ -0,0 +1,26 @@
+/**
+ * Copyright 2017 ZTE Corporation.
+ *
+ * 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.openo.dropwizard.ioc.test.service.conf;
+
+import io.dropwizard.Configuration;
+
+/**
+ * @author hu.rui
+ *
+ */
+public class TestConfiguration extends Configuration {
+
+}
diff --git a/wso2/dropwizard-ioc-container/src/test/resources/example.yml b/wso2/dropwizard-ioc-container/src/test/resources/example.yml
new file mode 100644
index 0000000..c3ef0f0
--- /dev/null
+++ b/wso2/dropwizard-ioc-container/src/test/resources/example.yml
@@ -0,0 +1,8 @@
+server:
+ type: simple
+ rootPath: '/service/*'
+ applicationContextPath: /
+ connector:
+ type: http
+ port: 45673
+ \ No newline at end of file