diff --git a/apm-collector/apm-collector-modulization-core/src/main/java/org/skywalking/apm/collector/modulization/ApplicationConfiguration.java b/apm-collector/apm-collector-modulization-core/src/main/java/org/skywalking/apm/collector/modulization/ApplicationConfiguration.java index 5984f32a0..d0a7db7e4 100644 --- a/apm-collector/apm-collector-modulization-core/src/main/java/org/skywalking/apm/collector/modulization/ApplicationConfiguration.java +++ b/apm-collector/apm-collector-modulization-core/src/main/java/org/skywalking/apm/collector/modulization/ApplicationConfiguration.java @@ -21,6 +21,11 @@ package org.skywalking.apm.collector.modulization; import java.util.HashMap; import java.util.Properties; +/** + * Modulization configurations. The {@link ModuleManager} is going to start, lookup, init modules based on this. + * + * @author wu-sheng + */ public class ApplicationConfiguration { private HashMap modules = new HashMap<>(); @@ -44,8 +49,17 @@ public class ApplicationConfiguration { public class ModuleConfiguration { private HashMap providers = new HashMap<>(); + private ModuleConfiguration() { + } + public Properties getProviderConfiguration(String name) { - return providers.get(name).properties; + return providers.get(name).getProperties(); + } + + public ModuleConfiguration addProviderConfiguration(String name, Properties properties) { + ProviderConfiguration newProvider = new ProviderConfiguration(properties); + providers.put(name, newProvider); + return this; } } @@ -54,5 +68,13 @@ public class ApplicationConfiguration { */ public class ProviderConfiguration { private Properties properties; + + ProviderConfiguration(Properties properties) { + this.properties = properties; + } + + public Properties getProperties() { + return properties; + } } } diff --git a/apm-collector/apm-collector-modulization-core/src/main/java/org/skywalking/apm/collector/modulization/Module.java b/apm-collector/apm-collector-modulization-core/src/main/java/org/skywalking/apm/collector/modulization/Module.java index fd0d18b5a..291a63eed 100644 --- a/apm-collector/apm-collector-modulization-core/src/main/java/org/skywalking/apm/collector/modulization/Module.java +++ b/apm-collector/apm-collector-modulization-core/src/main/java/org/skywalking/apm/collector/modulization/Module.java @@ -48,7 +48,7 @@ public abstract class Module { * @throws ProviderNotFoundException when even don't find a single one providers. */ void prepare(ModuleManager moduleManager, - ApplicationConfiguration.ModuleConfiguration configuration) throws ProviderNotFoundException { + ApplicationConfiguration.ModuleConfiguration configuration) throws ProviderNotFoundException, ServiceNotProvidedException { ServiceLoader moduleProviderLoader = ServiceLoader.load(ModuleProvider.class); boolean providerExist = false; for (ModuleProvider provider : moduleProviderLoader) { diff --git a/apm-collector/apm-collector-modulization-core/src/main/java/org/skywalking/apm/collector/modulization/ModuleManager.java b/apm-collector/apm-collector-modulization-core/src/main/java/org/skywalking/apm/collector/modulization/ModuleManager.java index e25a14cc2..411eb830a 100644 --- a/apm-collector/apm-collector-modulization-core/src/main/java/org/skywalking/apm/collector/modulization/ModuleManager.java +++ b/apm-collector/apm-collector-modulization-core/src/main/java/org/skywalking/apm/collector/modulization/ModuleManager.java @@ -54,7 +54,7 @@ public class ModuleManager { throw new ModuleNotFoundException(e); } newInstance.prepare(this, applicationConfiguration.getModuleConfiguration(moduleName)); - loadedModules.put(moduleName, module); + loadedModules.put(moduleName, newInstance); moduleList.remove(moduleName); } } diff --git a/apm-collector/apm-collector-modulization-core/src/main/java/org/skywalking/apm/collector/modulization/ModuleProvider.java b/apm-collector/apm-collector-modulization-core/src/main/java/org/skywalking/apm/collector/modulization/ModuleProvider.java index 74bc18265..6c82aced2 100644 --- a/apm-collector/apm-collector-modulization-core/src/main/java/org/skywalking/apm/collector/modulization/ModuleProvider.java +++ b/apm-collector/apm-collector-modulization-core/src/main/java/org/skywalking/apm/collector/modulization/ModuleProvider.java @@ -60,14 +60,14 @@ public abstract class ModuleProvider { * * @param config from `application.yml` */ - public abstract void prepare(Properties config); + public abstract void prepare(Properties config) throws ServiceNotProvidedException; /** * In prepare stage, the module can interop with other modules. * * @param config from `application.yml` */ - public abstract void init(Properties config); + public abstract void init(Properties config) throws ServiceNotProvidedException; /** * @return module names which does this module require? @@ -80,8 +80,13 @@ public abstract class ModuleProvider { * @param serviceType * @param service */ - protected void registerServiceImplementation(Class serviceType, Service service) { - this.services.put(serviceType, service); + protected void registerServiceImplementation(Class serviceType, + Service service) throws ServiceNotProvidedException { + if (serviceType.isInstance(service)) { + this.services.put(serviceType, service); + } else { + throw new ServiceNotProvidedException(serviceType + " is not implemented by " + service); + } } /** @@ -94,14 +99,14 @@ public abstract class ModuleProvider { if (requiredServices == null) return; - if (requiredServices.length != services.size()) { - throw new ServiceNotProvidedException("Haven't provided enough plugins."); - } - for (Class service : requiredServices) { if (!services.containsKey(service)) { throw new ServiceNotProvidedException("Service:" + service.getName() + " not provided"); } } + + if (requiredServices.length != services.size()) { + throw new ServiceNotProvidedException("Provide more service implementations than Module requirements."); + } } } diff --git a/apm-collector/apm-collector-modulization-core/src/test/java/org/skywalking/apm/collector/modulization/ApplicationConfigurationTest.java b/apm-collector/apm-collector-modulization-core/src/test/java/org/skywalking/apm/collector/modulization/ApplicationConfigurationTest.java new file mode 100644 index 000000000..73296d370 --- /dev/null +++ b/apm-collector/apm-collector-modulization-core/src/test/java/org/skywalking/apm/collector/modulization/ApplicationConfigurationTest.java @@ -0,0 +1,41 @@ +/* + * Copyright 2017, OpenSkywalking Organization All rights reserved. + * + * 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 repository: https://github.com/OpenSkywalking/skywalking + */ + +package org.skywalking.apm.collector.modulization; + +import java.util.Properties; +import org.junit.Assert; +import org.junit.Test; + +public class ApplicationConfigurationTest { + @Test + public void testBuildConfig() { + ApplicationConfiguration configuration = new ApplicationConfiguration(); + Properties p1 = new Properties(); + p1.setProperty("p1", "value1"); + p1.setProperty("p2", "value2"); + Properties p2 = new Properties(); + p2.setProperty("prop1", "value1-prop"); + p2.setProperty("prop2", "value2-prop"); + configuration.addModule("MO-1").addProviderConfiguration("MO-1-P1", p1).addProviderConfiguration("MO-1-P2", p2); + + Assert.assertArrayEquals(new String[] {"MO-1"}, configuration.moduleList()); + Assert.assertEquals("value2-prop", configuration.getModuleConfiguration("MO-1").getProviderConfiguration("MO-1-P2").getProperty("prop2")); + Assert.assertEquals(p1, configuration.getModuleConfiguration("MO-1").getProviderConfiguration("MO-1-P1")); + } +} diff --git a/apm-collector/apm-collector-modulization-core/src/test/java/org/skywalking/apm/collector/modulization/BaseModuleA.java b/apm-collector/apm-collector-modulization-core/src/test/java/org/skywalking/apm/collector/modulization/BaseModuleA.java new file mode 100644 index 000000000..18f9638e0 --- /dev/null +++ b/apm-collector/apm-collector-modulization-core/src/test/java/org/skywalking/apm/collector/modulization/BaseModuleA.java @@ -0,0 +1,40 @@ +/* + * Copyright 2017, OpenSkywalking Organization All rights reserved. + * + * 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 repository: https://github.com/OpenSkywalking/skywalking + */ + +package org.skywalking.apm.collector.modulization; + +/** + * @author wu-sheng + */ +public class BaseModuleA extends Module { + @Override public String name() { + return "BaseA"; + } + + @Override public Class[] services() { + return new Class[] {ServiceABusiness1.class, ServiceABusiness2.class}; + } + + public interface ServiceABusiness1 extends Service { + + } + + public interface ServiceABusiness2 extends Service { + + } +} diff --git a/apm-collector/apm-collector-modulization-core/src/test/java/org/skywalking/apm/collector/modulization/BaseModuleB.java b/apm-collector/apm-collector-modulization-core/src/test/java/org/skywalking/apm/collector/modulization/BaseModuleB.java new file mode 100644 index 000000000..bfe412a03 --- /dev/null +++ b/apm-collector/apm-collector-modulization-core/src/test/java/org/skywalking/apm/collector/modulization/BaseModuleB.java @@ -0,0 +1,40 @@ +/* + * Copyright 2017, OpenSkywalking Organization All rights reserved. + * + * 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 repository: https://github.com/OpenSkywalking/skywalking + */ + +package org.skywalking.apm.collector.modulization; + +/** + * @author wu-sheng + */ +public class BaseModuleB extends Module { + @Override public String name() { + return "BaseB"; + } + + @Override public Class[] services() { + return new Class[] {BaseModuleB.ServiceBBusiness1.class, BaseModuleB.ServiceBBusiness2.class}; + } + + public interface ServiceBBusiness1 extends Service { + + } + + public interface ServiceBBusiness2 extends Service { + + } +} diff --git a/apm-collector/apm-collector-modulization-core/src/test/java/org/skywalking/apm/collector/modulization/ModuleAProvider.java b/apm-collector/apm-collector-modulization-core/src/test/java/org/skywalking/apm/collector/modulization/ModuleAProvider.java new file mode 100644 index 000000000..54ee3f02d --- /dev/null +++ b/apm-collector/apm-collector-modulization-core/src/test/java/org/skywalking/apm/collector/modulization/ModuleAProvider.java @@ -0,0 +1,54 @@ +/* + * Copyright 2017, OpenSkywalking Organization All rights reserved. + * + * 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 repository: https://github.com/OpenSkywalking/skywalking + */ + +package org.skywalking.apm.collector.modulization; + +import java.util.Properties; + +/** + * @author wu-sheng + */ +public class ModuleAProvider extends ModuleProvider { + @Override public String name() { + return "P-A"; + } + + @Override public Class module() { + return BaseModuleA.class; + } + + @Override public void prepare(Properties config) throws ServiceNotProvidedException { + this.registerServiceImplementation(BaseModuleA.ServiceABusiness1.class, new Business1()); + } + + @Override public void init(Properties config) throws ServiceNotProvidedException { + this.registerServiceImplementation(BaseModuleA.ServiceABusiness2.class, new Business2()); + } + + @Override public String[] requiredModules() { + return new String[0]; + } + + public class Business1 implements BaseModuleA.ServiceABusiness1 { + + } + + public class Business2 implements BaseModuleA.ServiceABusiness2 { + + } +} diff --git a/apm-collector/apm-collector-modulization-core/src/test/java/org/skywalking/apm/collector/modulization/ModuleBProvider.java b/apm-collector/apm-collector-modulization-core/src/test/java/org/skywalking/apm/collector/modulization/ModuleBProvider.java new file mode 100644 index 000000000..e49c75035 --- /dev/null +++ b/apm-collector/apm-collector-modulization-core/src/test/java/org/skywalking/apm/collector/modulization/ModuleBProvider.java @@ -0,0 +1,54 @@ +/* + * Copyright 2017, OpenSkywalking Organization All rights reserved. + * + * 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 repository: https://github.com/OpenSkywalking/skywalking + */ + +package org.skywalking.apm.collector.modulization; + +import java.util.Properties; + +/** + * @author wu-sheng + */ +public class ModuleBProvider extends ModuleProvider { + @Override public String name() { + return "P-B"; + } + + @Override public Class module() { + return BaseModuleB.class; + } + + @Override public void prepare(Properties config) throws ServiceNotProvidedException { + this.registerServiceImplementation(BaseModuleB.ServiceBBusiness1.class, new Business1()); + } + + @Override public void init(Properties config) throws ServiceNotProvidedException { + this.registerServiceImplementation(BaseModuleB.ServiceBBusiness2.class, new Business2()); + } + + @Override public String[] requiredModules() { + return new String[0]; + } + + public class Business1 implements BaseModuleB.ServiceBBusiness1 { + + } + + public class Business2 implements BaseModuleB.ServiceBBusiness2 { + + } +} diff --git a/apm-collector/apm-collector-modulization-core/src/test/java/org/skywalking/apm/collector/modulization/ModuleManagerTest.java b/apm-collector/apm-collector-modulization-core/src/test/java/org/skywalking/apm/collector/modulization/ModuleManagerTest.java new file mode 100644 index 000000000..30e33969a --- /dev/null +++ b/apm-collector/apm-collector-modulization-core/src/test/java/org/skywalking/apm/collector/modulization/ModuleManagerTest.java @@ -0,0 +1,37 @@ +/* + * Copyright 2017, OpenSkywalking Organization All rights reserved. + * + * 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 repository: https://github.com/OpenSkywalking/skywalking + */ + +package org.skywalking.apm.collector.modulization; + +import org.junit.Test; + +/** + * @author wu-sheng + */ +public class ModuleManagerTest { + @Test + public void testInit() throws ServiceNotProvidedException, ModuleNotFoundException, ProviderNotFoundException { + ApplicationConfiguration configuration = new ApplicationConfiguration(); + configuration.addModule("Test").addProviderConfiguration("TestModule-Provider", null); + configuration.addModule("BaseA").addProviderConfiguration("P-A",null); + configuration.addModule("BaseB").addProviderConfiguration("P-B",null); + + ModuleManager manager = new ModuleManager(); + manager.init(configuration); + } +} diff --git a/apm-collector/apm-collector-modulization-core/src/test/java/org/skywalking/apm/collector/modulization/TestModule.java b/apm-collector/apm-collector-modulization-core/src/test/java/org/skywalking/apm/collector/modulization/TestModule.java new file mode 100644 index 000000000..fe59136bf --- /dev/null +++ b/apm-collector/apm-collector-modulization-core/src/test/java/org/skywalking/apm/collector/modulization/TestModule.java @@ -0,0 +1,32 @@ +/* + * Copyright 2017, OpenSkywalking Organization All rights reserved. + * + * 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 repository: https://github.com/OpenSkywalking/skywalking + */ + +package org.skywalking.apm.collector.modulization; + +/** + * @author wu-sheng + */ +public class TestModule extends Module { + @Override public String name() { + return "Test"; + } + + @Override public Class[] services() { + return new Class[0]; + } +} diff --git a/apm-collector/apm-collector-modulization-core/src/test/java/org/skywalking/apm/collector/modulization/TestModuleProvider.java b/apm-collector/apm-collector-modulization-core/src/test/java/org/skywalking/apm/collector/modulization/TestModuleProvider.java new file mode 100644 index 000000000..590f5c7b3 --- /dev/null +++ b/apm-collector/apm-collector-modulization-core/src/test/java/org/skywalking/apm/collector/modulization/TestModuleProvider.java @@ -0,0 +1,46 @@ +/* + * Copyright 2017, OpenSkywalking Organization All rights reserved. + * + * 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 repository: https://github.com/OpenSkywalking/skywalking + */ + +package org.skywalking.apm.collector.modulization; + +import java.util.Properties; + +/** + * @author wu-sheng + */ +public class TestModuleProvider extends ModuleProvider { + @Override public String name() { + return "TestModule-Provider"; + } + + @Override public Class module() { + return TestModule.class; + } + + @Override public void prepare(Properties config) { + + } + + @Override public void init(Properties config) { + + } + + @Override public String[] requiredModules() { + return new String[] {"BaseA", "BaseB"}; + } +} diff --git a/apm-collector/apm-collector-modulization-core/src/test/resources/META-INF/services/org.skywalking.apm.collector.modulization.Module b/apm-collector/apm-collector-modulization-core/src/test/resources/META-INF/services/org.skywalking.apm.collector.modulization.Module new file mode 100644 index 000000000..56aa25556 --- /dev/null +++ b/apm-collector/apm-collector-modulization-core/src/test/resources/META-INF/services/org.skywalking.apm.collector.modulization.Module @@ -0,0 +1,21 @@ +# +# Copyright 2017, OpenSkywalking Organization All rights reserved. +# +# 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 repository: https://github.com/OpenSkywalking/skywalking +# + +org.skywalking.apm.collector.modulization.TestModule +org.skywalking.apm.collector.modulization.BaseModuleA +org.skywalking.apm.collector.modulization.BaseModuleB \ No newline at end of file diff --git a/apm-collector/apm-collector-modulization-core/src/test/resources/META-INF/services/org.skywalking.apm.collector.modulization.ModuleProvider b/apm-collector/apm-collector-modulization-core/src/test/resources/META-INF/services/org.skywalking.apm.collector.modulization.ModuleProvider new file mode 100644 index 000000000..29fc1aadb --- /dev/null +++ b/apm-collector/apm-collector-modulization-core/src/test/resources/META-INF/services/org.skywalking.apm.collector.modulization.ModuleProvider @@ -0,0 +1,21 @@ +# +# Copyright 2017, OpenSkywalking Organization All rights reserved. +# +# 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 repository: https://github.com/OpenSkywalking/skywalking +# + +org.skywalking.apm.collector.modulization.TestModuleProvider +org.skywalking.apm.collector.modulization.ModuleAProvider +org.skywalking.apm.collector.modulization.ModuleBProvider \ No newline at end of file