提交 ca547c45 编写于 作者: wu-sheng's avatar wu-sheng

Fix the codes of modulization core.

上级 66e553a4
......@@ -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<String, ModuleConfiguration> modules = new HashMap<>();
......@@ -44,8 +49,17 @@ public class ApplicationConfiguration {
public class ModuleConfiguration {
private HashMap<String, ProviderConfiguration> 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;
}
}
}
......@@ -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<ModuleProvider> moduleProviderLoader = ServiceLoader.load(ModuleProvider.class);
boolean providerExist = false;
for (ModuleProvider provider : moduleProviderLoader) {
......
......@@ -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);
}
}
......
......@@ -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<? extends Service> serviceType, Service service) {
this.services.put(serviceType, service);
protected void registerServiceImplementation(Class<? extends Service> 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<? extends Service> 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.");
}
}
}
/*
* 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"));
}
}
/*
* 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<? extends Service>[] services() {
return new Class[] {ServiceABusiness1.class, ServiceABusiness2.class};
}
public interface ServiceABusiness1 extends Service {
}
public interface ServiceABusiness2 extends Service {
}
}
/*
* 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<? extends Service>[] services() {
return new Class[] {BaseModuleB.ServiceBBusiness1.class, BaseModuleB.ServiceBBusiness2.class};
}
public interface ServiceBBusiness1 extends Service {
}
public interface ServiceBBusiness2 extends Service {
}
}
/*
* 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<? extends Module> 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 {
}
}
/*
* 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<? extends Module> 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 {
}
}
/*
* 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);
}
}
/*
* 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<? extends Service>[] services() {
return new Class[0];
}
}
/*
* 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<? extends Module> 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"};
}
}
#
# 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
#
# 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
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册