View Javadoc

1   /*******************************************************************************
2    * Copyright (c) 2007, 2014 Massimiliano Ziccardi
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   *******************************************************************************/
16  package it.jnrpe.osgi;
17  
18  import it.jnrpe.commands.CommandDefinition;
19  import it.jnrpe.commands.CommandRepository;
20  import it.jnrpe.plugins.IPluginRepository;
21  import it.jnrpe.plugins.PluginDefinition;
22  import it.jnrpe.utils.PluginRepositoryUtil;
23  import it.jnrpe.utils.StreamManager;
24  
25  import java.io.InputStream;
26  import java.security.AccessController;
27  import java.security.PrivilegedAction;
28  import java.util.Collection;
29  
30  import org.osgi.framework.Bundle;
31  import org.osgi.framework.BundleContext;
32  import org.osgi.framework.BundleEvent;
33  import org.osgi.util.tracker.BundleTracker;
34  import org.slf4j.Logger;
35  import org.slf4j.LoggerFactory;
36  
37  public class JNRPEBundleTracker extends BundleTracker {
38  
39  	private final static String JNRPE_PLUGIN_PACKAGE_NAME = "JNRPE-PluginPackage-Name";
40  
41  	/**
42  	 * The logger.
43  	 */
44  	private static final Logger LOG = LoggerFactory
45  			.getLogger(JNRPEBundleTracker.class);
46  
47  	private final IPluginRepository pluginRepository;
48  	private final CommandRepository commandRepository;
49  
50  	public JNRPEBundleTracker(final BundleContext context,
51  			final IPluginRepository pluginRepo,
52  			final CommandRepository commandRepo) {
53  		super(context, Bundle.ACTIVE | Bundle.STOPPING, null);
54  		pluginRepository = pluginRepo;
55  		commandRepository = commandRepo;
56  	}
57  
58  	private InputStream getPluginConfigStream(ClassLoader cl) {
59  		InputStream in = null;
60  
61  		in = cl.getResourceAsStream("jnrpe_plugins.xml");
62  
63  		if (in == null) {
64  			// fallback to the old plugin.xml file...
65  			in = cl.getResourceAsStream("plugin.xml");
66  		}
67  
68  		return in;
69  	}
70  
71  	@Override
72  	public Object addingBundle(final Bundle bundle, final BundleEvent event) {
73  
74  		String pluginPackageClassName = (String) bundle.getHeaders().get(
75  				JNRPE_PLUGIN_PACKAGE_NAME);
76  
77  		if (pluginPackageClassName != null) {
78  			LOG.info("Plugin package found: {} ", pluginPackageClassName);
79  
80  			StreamManager sm = new StreamManager();
81  			// The bundle is a plugin package...
82  			try {
83  				BundleDelegatingClassLoader bdc = AccessController
84  						.doPrivileged(new PrivilegedAction<BundleDelegatingClassLoader>() {
85  							public BundleDelegatingClassLoader run() {
86  								return new BundleDelegatingClassLoader(bundle);
87  							}
88  						});
89  
90  				// the 'plugin.xml' file is deprecated. Search for the new
91  				// jnrpe_plugins.xml file and then fallback to the old
92  				// plugin.xml
93  
94  				InputStream in = sm.handle(getPluginConfigStream(bdc));
95  
96  				// FIXME: check that 'in' is not null
97  
98  				Collection<PluginDefinition> pdList = PluginRepositoryUtil
99  						.loadFromXmlPluginPackageDefinitions(bdc, in);
100 
101 				for (PluginDefinition pd : pdList) {
102 					LOG.info("Adding plugin '{}' to the repository",
103 							pd.getName());
104 					pluginRepository.addPluginDefinition(pd);
105 				}
106 
107 			} catch (Exception e) {
108 				LOG.error("Error adding plugin to the repository", e);
109 			} finally {
110 				sm.closeAll();
111 			}
112 		}
113 
114 		return bundle;
115 	}
116 
117 	@Override
118 	public void remove(final Bundle bundle) {
119 		String pluginPackageClassName = (String) bundle.getHeaders().get(
120 				JNRPE_PLUGIN_PACKAGE_NAME);
121 
122 		if (pluginPackageClassName != null) {
123 
124 			StreamManager sm = new StreamManager();
125 
126 			// The bundle is a plugin package...
127 			try {
128 				BundleDelegatingClassLoader bdc = AccessController
129 						.doPrivileged(new PrivilegedAction<BundleDelegatingClassLoader>() {
130 							public BundleDelegatingClassLoader run() {
131 								return new BundleDelegatingClassLoader(bundle);
132 							}
133 						});
134 
135 				InputStream in = sm.handle(getPluginConfigStream(bdc));
136 
137 				Collection<PluginDefinition> pdList = PluginRepositoryUtil
138 						.loadFromXmlPluginPackageDefinitions(bdc, in);
139 
140 				for (PluginDefinition pd : pdList) {
141 					// First remove all the commands using the plugin...
142 					for (CommandDefinition cd : commandRepository
143 							.getAllCommandDefinition(pd.getName())) {
144 						LOG.info("Removing command '{}' from the repository",
145 								cd.getName());
146 						commandRepository.removeCommandDefinition(cd);
147 					}
148 
149 					LOG.info("Removing plugin '{}' from the repository",
150 							pd.getName());
151 					pluginRepository.removePluginDefinition(pd);
152 				}
153 			} catch (Exception e) {
154 				LOG.error("Error removing plugin from the repository", e);
155 			} finally {
156 				sm.closeAll();
157 			}
158 		}
159 	}
160 }