1 package com.atlassian.plugin.impl;
2
3 import com.atlassian.plugin.classloader.PluginClassLoader;
4 import com.atlassian.plugin.loaders.classloading.DeploymentUnit;
5 import com.atlassian.plugin.PluginArtifact;
6 import com.atlassian.plugin.JarPluginArtifact;
7 import com.atlassian.plugin.PluginState;
8
9 import java.io.InputStream;
10 import java.net.URL;
11
12
13
14
15 public class DefaultDynamicPlugin extends AbstractPlugin implements DynamicPlugin
16 {
17 private final PluginArtifact pluginArtifact;
18 private final PluginClassLoader loader;
19 private boolean deletable = true;
20 private boolean bundled = false;
21 private boolean closed = false;
22
23 public DefaultDynamicPlugin(final DeploymentUnit deploymentUnit, final PluginClassLoader loader)
24 {
25 this(new JarPluginArtifact(deploymentUnit.getPath()), loader);
26 }
27
28 public DefaultDynamicPlugin(final PluginArtifact pluginArtifact, final PluginClassLoader loader)
29 {
30 if (loader == null)
31 {
32 throw new IllegalArgumentException("PluginClassLoader must not be null");
33 }
34 this.pluginArtifact = pluginArtifact;
35 this.loader = loader;
36 }
37
38 public <T> Class<T> loadClass(final String clazz, final Class<?> callingClass) throws ClassNotFoundException
39 {
40 @SuppressWarnings("unchecked")
41 final Class<T> result = (Class<T>) loader.loadClass(clazz);
42 return result;
43 }
44
45 public boolean isUninstallable()
46 {
47 return true;
48 }
49
50 public URL getResource(final String name)
51 {
52 return loader.getResource(name);
53 }
54
55 public InputStream getResourceAsStream(final String name)
56 {
57 return loader.getResourceAsStream(name);
58 }
59
60 public ClassLoader getClassLoader()
61 {
62 return loader;
63 }
64
65
66
67
68
69
70 public boolean isDynamicallyLoaded()
71 {
72 return true;
73 }
74
75
76
77
78 public DeploymentUnit getDeploymentUnit()
79 {
80 return new DeploymentUnit(pluginArtifact.toFile());
81 }
82
83
84
85
86 public PluginArtifact getPluginArtifact()
87 {
88 return pluginArtifact;
89 }
90
91 public boolean isDeleteable()
92 {
93 return deletable;
94 }
95
96 public void setDeletable(final boolean deletable)
97 {
98 this.deletable = deletable;
99 }
100
101 public boolean isBundledPlugin()
102 {
103 return bundled;
104 }
105
106 public void setBundled(final boolean bundled)
107 {
108 this.bundled = bundled;
109 }
110
111 public void close()
112 {
113 loader.close();
114 closed = true;
115 }
116
117 @Override
118 public PluginState getPluginState()
119 {
120 return (closed ? PluginState.CLOSED : super.getPluginState());
121 }
122 }