View Javadoc

1   package com.atlassian.plugin.osgi.factory;
2   
3   import com.atlassian.plugin.ModuleDescriptor;
4   import com.atlassian.plugin.PluginException;
5   import com.atlassian.plugin.PluginInformation;
6   import com.atlassian.plugin.PluginState;
7   import com.atlassian.plugin.Resourced;
8   import com.atlassian.plugin.elements.ResourceDescriptor;
9   import com.atlassian.plugin.elements.ResourceLocation;
10  import com.atlassian.plugin.event.PluginEventManager;
11  import com.atlassian.plugin.impl.AbstractPlugin;
12  import com.atlassian.plugin.util.resource.AlternativeDirectoryResourceLoader;
13  
14  import org.osgi.framework.Bundle;
15  import org.osgi.framework.BundleException;
16  import org.osgi.framework.Constants;
17  
18  import java.io.InputStream;
19  import java.net.URL;
20  import java.util.Collection;
21  import java.util.Collections;
22  import java.util.Date;
23  import java.util.List;
24  
25  /**
26   * Plugin that wraps an OSGi bundle that has no plugin descriptor.
27   */
28  public class OsgiBundlePlugin extends AbstractPlugin
29  {
30  
31      private final Bundle bundle;
32      private final PluginInformation pluginInformation;
33      private final Date dateLoaded;
34      private final String key;
35      private final ClassLoader bundleClassLoader;
36  
37      public OsgiBundlePlugin(final Bundle bundle, final String key, final PluginEventManager pluginEventManager)
38      {
39          bundleClassLoader = BundleClassLoaderAccessor.getClassLoader(bundle, new AlternativeDirectoryResourceLoader());
40          this.bundle = bundle;
41          pluginInformation = new PluginInformation();
42          pluginInformation.setDescription((String) bundle.getHeaders().get(Constants.BUNDLE_DESCRIPTION));
43          pluginInformation.setVersion((String) bundle.getHeaders().get(Constants.BUNDLE_VERSION));
44          this.key = key;
45          dateLoaded = new Date();
46      }
47  
48  
49      @Override
50      public int getPluginsVersion()
51      {
52          return 2;
53      }
54  
55      @Override
56      public void setPluginsVersion(final int version)
57      {
58          throw new UnsupportedOperationException("Not available");
59      }
60  
61      @Override
62      public String getName()
63      {
64          return (String) bundle.getHeaders().get("Bundle-Name");
65      }
66  
67      @Override
68      public void setName(final String name)
69      {
70          throw new UnsupportedOperationException("Not available");
71      }
72  
73      @Override
74      public String getI18nNameKey()
75      {
76          return key;
77      }
78  
79      @Override
80      public void setI18nNameKey(final String i18nNameKey)
81      {
82          throw new UnsupportedOperationException("Not available");
83      }
84  
85      @Override
86      public String getKey()
87      {
88          return key;
89      }
90  
91      @Override
92      public void setKey(final String aPackage)
93      {
94          throw new UnsupportedOperationException("Not available");
95      }
96  
97      @Override
98      public void addModuleDescriptor(final ModuleDescriptor<?> moduleDescriptor)
99      {
100         throw new UnsupportedOperationException("Not available");
101     }
102 
103     @Override
104     public Collection<ModuleDescriptor<?>> getModuleDescriptors()
105     {
106         return Collections.emptyList();
107     }
108 
109     @Override
110     public ModuleDescriptor<?> getModuleDescriptor(final String key)
111     {
112         return null;
113     }
114 
115     @Override
116     public <M> List<ModuleDescriptor<M>> getModuleDescriptorsByModuleClass(final Class<M> aClass)
117     {
118         return Collections.emptyList();
119     }
120 
121     @Override
122     public boolean isEnabledByDefault()
123     {
124         return true;
125     }
126 
127     @Override
128     public void setEnabledByDefault(final boolean enabledByDefault)
129     {
130         throw new UnsupportedOperationException("Not available");
131     }
132 
133     @Override
134     public PluginInformation getPluginInformation()
135     {
136         return pluginInformation;
137     }
138 
139     @Override
140     public void setPluginInformation(final PluginInformation pluginInformation)
141     {
142         throw new UnsupportedOperationException("Not available");
143     }
144 
145     @Override
146     public void setResources(final Resourced resources)
147     {
148         throw new UnsupportedOperationException("Not available");
149     }
150 
151     @Override
152     public boolean isSystemPlugin()
153     {
154         return false;
155     }
156 
157     @Override
158     public boolean containsSystemModule()
159     {
160         return false;
161     }
162 
163     @Override
164     public void setSystemPlugin(final boolean system)
165     {
166         throw new UnsupportedOperationException("Not available");
167     }
168 
169     @Override
170     public Date getDateLoaded()
171     {
172         return dateLoaded;
173     }
174 
175     public boolean isUninstallable()
176     {
177         return true;
178     }
179 
180     public boolean isDeleteable()
181     {
182         return true;
183     }
184 
185     public boolean isDynamicallyLoaded()
186     {
187         return true;
188     }
189 
190 
191     @Override
192     public List<ResourceDescriptor> getResourceDescriptors()
193     {
194         return Collections.emptyList();
195     }
196 
197     @Override
198     public List<ResourceDescriptor> getResourceDescriptors(final String type)
199     {
200         return Collections.emptyList();
201     }
202 
203     @Override
204     public ResourceLocation getResourceLocation(final String type, final String name)
205     {
206         return null;
207     }
208 
209     @Override
210     public ResourceDescriptor getResourceDescriptor(final String type, final String name)
211     {
212         return null;
213     }
214 
215     public <T> Class<T> loadClass(final String clazz, final Class<?> callingClass) throws ClassNotFoundException
216     {
217         return BundleClassLoaderAccessor.loadClass(bundle, clazz, callingClass);
218     }
219 
220     public URL getResource(final String name)
221     {
222         return bundleClassLoader.getResource(name);
223     }
224 
225     public InputStream getResourceAsStream(final String name)
226     {
227         return bundleClassLoader.getResourceAsStream(name);
228     }
229 
230     @Override
231     protected void uninstallInternal()
232     {
233         try
234         {
235             bundle.uninstall();
236         }
237         catch (final BundleException e)
238         {
239             throw new PluginException(e);
240         }
241     }
242 
243     @Override
244     protected PluginState enableInternal()
245     {
246         try
247         {
248             bundle.start();
249             return PluginState.ENABLED;
250         }
251         catch (final BundleException e)
252         {
253             throw new PluginException(e);
254         }
255     }
256 
257     @Override
258     protected void disableInternal()
259     {
260         try
261         {
262             bundle.stop();
263         }
264         catch (final BundleException e)
265         {
266             throw new PluginException(e);
267         }
268     }
269 
270     public ClassLoader getClassLoader()
271     {
272         return bundleClassLoader;
273     }
274 
275 }