Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
PluginEnvironment |
|
| 1.25;1.25 |
1 | /* | |
2 | * Copyright 2005-2007 The Kuali Foundation | |
3 | * | |
4 | * | |
5 | * Licensed under the Educational Community License, Version 2.0 (the "License"); | |
6 | * you may not use this file except in compliance with the License. | |
7 | * You may obtain a copy of the License at | |
8 | * | |
9 | * http://www.opensource.org/licenses/ecl2.php | |
10 | * | |
11 | * Unless required by applicable law or agreed to in writing, software | |
12 | * distributed under the License is distributed on an "AS IS" BASIS, | |
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
14 | * See the License for the specific language governing permissions and | |
15 | * limitations under the License. | |
16 | */ | |
17 | package org.kuali.rice.kew.plugin; | |
18 | ||
19 | /** | |
20 | * A PluginEnvironment represents a Plugin and the PluginLoader from which it was loaded. | |
21 | * Grouping these together allows us to execute a reload of the Plugin since the Plugin | |
22 | * itself is not responsible for handling how it's own loading or reloading. | |
23 | * | |
24 | * <p>The PluginEnvironment also keeps a reference to the PluginRegistry because this | |
25 | * allows it to add and remove Plugins as child resource loaders of the registry. | |
26 | * | |
27 | * @author Kuali Rice Team (rice.collab@kuali.org) | |
28 | */ | |
29 | public class PluginEnvironment implements Reloadable { | |
30 | ||
31 | 0 | private boolean loaded = false; |
32 | private Plugin plugin; | |
33 | private final PluginLoader loader; | |
34 | private final PluginRegistry registry; | |
35 | 0 | private boolean supressStartupFailure = true; |
36 | ||
37 | /** | |
38 | * Constructs an unloaded PluginEnvironment from the given PluginLoader and PluginRegistry. | |
39 | * The environment will not be loaded until the first time that load() is executed. | |
40 | */ | |
41 | 0 | public PluginEnvironment(PluginLoader loader, PluginRegistry registry) { |
42 | 0 | this.loader = loader; |
43 | 0 | this.registry = registry; |
44 | 0 | } |
45 | ||
46 | /** | |
47 | * Constructs a PluginEnvironment representing the given loaded Plugin. Environments created | |
48 | * in this manner will indicate that they are loaded from the moment they are constructed. | |
49 | */ | |
50 | public PluginEnvironment(Plugin plugin, PluginLoader loader, PluginRegistry registry) { | |
51 | 0 | this(loader, registry); |
52 | 0 | this.plugin = plugin; |
53 | 0 | this.loaded = true; |
54 | 0 | } |
55 | ||
56 | /** | |
57 | * Returns true if this environment has been loaded, false otherwise. If this method returns | |
58 | * false then getPlugin() may return null if the environment was never loaded after construction. | |
59 | */ | |
60 | public boolean isLoaded() { | |
61 | 0 | return loaded; |
62 | } | |
63 | ||
64 | /** | |
65 | * Returns a boolean indicating whether or not this PluginEnvironment is truly reloadable. | |
66 | * | |
67 | * This will return false if the Plugin represents a plugin which can not be reloaded. | |
68 | */ | |
69 | public boolean isReloadable() { | |
70 | 0 | return true; |
71 | } | |
72 | ||
73 | /** | |
74 | * Indicates whether or not a reload of this environment is needed. If this method | |
75 | * returns true, it should continue to return true until a reload() is executed. | |
76 | */ | |
77 | public synchronized boolean isReloadNeeded() { | |
78 | 0 | return loader.isModified(); |
79 | } | |
80 | ||
81 | /** | |
82 | * Reloads the environment by effectively executing an unload() followed by a load(). | |
83 | */ | |
84 | public synchronized void reload() throws Exception { | |
85 | 0 | unload(); |
86 | 0 | load(); |
87 | 0 | } |
88 | ||
89 | /** | |
90 | * Loads the plugin from the PluginLoader. This will also start the Plugin and add it | |
91 | * to the PluginRegistry's resoure loaders. | |
92 | */ | |
93 | public synchronized void load() throws Exception { | |
94 | 0 | plugin = loader.load(); |
95 | 0 | plugin.setSupressStartupFailure(supressStartupFailure); |
96 | // it's important that the plugin is added to the resource loading system prior to startup because | |
97 | // startup may need to grab services | |
98 | 0 | registry.addResourceLoader(plugin); |
99 | 0 | plugin.start(); |
100 | 0 | loaded = true; |
101 | 0 | } |
102 | ||
103 | /** | |
104 | * Unloads the plugin. The effectively shuts the plugin down and removes it from the | |
105 | * PluginRegistry's resource loaders. | |
106 | * @throws Exception | |
107 | */ | |
108 | public synchronized void unload() throws Exception { | |
109 | 0 | if (plugin != null) { |
110 | 0 | plugin.stop(); |
111 | // it's important that the plugin be removed from the resource loading system after shutdown in | |
112 | // case the plugin needs to access the resource loader during shutdown | |
113 | 0 | registry.removeResourceLoader(plugin.getName()); |
114 | } | |
115 | 0 | loaded = false; |
116 | 0 | } |
117 | ||
118 | public String getPluginName() { | |
119 | 0 | if (getPlugin() != null) { |
120 | 0 | return getPlugin().getName().getLocalPart(); |
121 | } | |
122 | 0 | return loader.getPluginName(); |
123 | } | |
124 | ||
125 | /** | |
126 | * Gets the Plugin represented by this environment. May be null if this environment has not | |
127 | * yet been loaded. | |
128 | */ | |
129 | public Plugin getPlugin() { | |
130 | 0 | return plugin; |
131 | } | |
132 | ||
133 | /** | |
134 | * Gets the PluginLoader which loaded the Plugin in this environment. | |
135 | */ | |
136 | public PluginLoader getLoader() { | |
137 | 0 | return loader; |
138 | } | |
139 | ||
140 | /** | |
141 | * By default, startup failure is supressed. If it is indicated that startup failure | |
142 | * supression is not desired, then startup errors will be thrown directly from calls to | |
143 | * load(). | |
144 | */ | |
145 | public void setSupressStartupFailure(boolean supressStartupFailure) { | |
146 | 0 | this.supressStartupFailure = supressStartupFailure; |
147 | 0 | } |
148 | ||
149 | } |