1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.seasar.uruma.rcp.core; |
17 | |
|
18 | |
import java.net.URL; |
19 | |
import java.util.ArrayList; |
20 | |
import java.util.Dictionary; |
21 | |
import java.util.Hashtable; |
22 | |
import java.util.List; |
23 | |
import java.util.Properties; |
24 | |
|
25 | |
import org.eclipse.core.internal.preferences.exchange.IProductPreferencesService; |
26 | |
import org.eclipse.core.internal.preferences.legacy.ProductPreferencesService; |
27 | |
import org.eclipse.swt.widgets.Display; |
28 | |
import org.eclipse.swt.widgets.Shell; |
29 | |
import org.osgi.framework.Bundle; |
30 | |
import org.osgi.framework.BundleActivator; |
31 | |
import org.osgi.framework.BundleContext; |
32 | |
import org.osgi.framework.Constants; |
33 | |
import org.osgi.framework.ServiceReference; |
34 | |
import org.seasar.uruma.core.UrumaConstants; |
35 | |
import org.seasar.uruma.core.UrumaMessageCodes; |
36 | |
import org.seasar.uruma.exception.UrumaAppInitException; |
37 | |
import org.seasar.uruma.exception.UrumaAppNotFoundException; |
38 | |
import org.seasar.uruma.log.UrumaLogger; |
39 | |
import org.seasar.uruma.rcp.UrumaService; |
40 | |
import org.seasar.uruma.rcp.core.UrumaBundleState.BundleState; |
41 | |
import org.seasar.uruma.rcp.util.BundleInfoUtil; |
42 | |
import org.seasar.uruma.rcp.util.BundleUtil; |
43 | |
import org.seasar.uruma.ui.dialogs.UrumaErrorDialog; |
44 | |
import org.seasar.uruma.util.MessageUtil; |
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | 0 | public class CoreActivator implements BundleActivator, UrumaConstants, |
52 | |
UrumaMessageCodes { |
53 | 0 | private static final UrumaLogger logger = UrumaLogger |
54 | |
.getLogger(CoreActivator.class); |
55 | |
|
56 | |
public void start(final BundleContext context) throws Exception { |
57 | 0 | logger.log(URUMA_BUNDLE_START); |
58 | |
|
59 | |
try { |
60 | 0 | context.addBundleListener(new UrumaBundleListener()); |
61 | |
|
62 | 0 | List<Bundle> activated = prepareUrumaService(context); |
63 | 0 | if (activated.size() == 0) { |
64 | 0 | throw new UrumaAppNotFoundException(); |
65 | |
} |
66 | |
|
67 | 0 | registerProductPreferenceService(context); |
68 | |
|
69 | 0 | UrumaBundleState.getInstance().setUrumaBundleState( |
70 | |
BundleState.AVAILABLE); |
71 | 0 | } catch (Throwable ex) { |
72 | 0 | Display display = new Display(); |
73 | 0 | Shell shell = new Shell(display); |
74 | 0 | String msg = MessageUtil.getMessageWithBundleName( |
75 | |
URUMA_MESSAGE_BASE, "RCP_START_FAILED"); |
76 | 0 | UrumaErrorDialog dialog = new UrumaErrorDialog(shell, "Uruma", msg, |
77 | |
ex); |
78 | 0 | dialog.open(); |
79 | 0 | shell.dispose(); |
80 | 0 | display.dispose(); |
81 | 0 | } |
82 | 0 | } |
83 | |
|
84 | |
public void stop(final BundleContext context) throws Exception { |
85 | 0 | UrumaBundleState.getInstance().setUrumaBundleState( |
86 | |
BundleState.NOT_AVAILABLE); |
87 | 0 | logger.log(URUMA_BUNDLE_STOP); |
88 | 0 | } |
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
@SuppressWarnings("unchecked") |
98 | |
protected List<Bundle> prepareUrumaService(final BundleContext context) { |
99 | 0 | List<Bundle> successedBundleList = new ArrayList<Bundle>(); |
100 | 0 | String serviceName = UrumaService.class.getName(); |
101 | 0 | List<Bundle> appBundles = findUrumaApplications(context); |
102 | 0 | if (appBundles.size() == 0) { |
103 | 0 | throw new UrumaAppNotFoundException(); |
104 | |
} |
105 | |
|
106 | 0 | Dictionary props = new Properties(); |
107 | 0 | props.put(URUMA_SERVICE_PROP_APPS, getBundleSymbolicNames(appBundles)); |
108 | |
|
109 | 0 | UrumaServiceFactory factory = new UrumaServiceFactory(); |
110 | 0 | context.registerService(serviceName, factory, props); |
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | 0 | for (Bundle bundle : appBundles) { |
117 | 0 | setupLog4jConfig(bundle); |
118 | 0 | BundleContext appContext = bundle.getBundleContext(); |
119 | 0 | if (appContext == null) { |
120 | 0 | logger.log(INVALID_URUMA_APP_BUNDLE, bundle.getSymbolicName(), |
121 | |
BundleUtil.getBundleState(bundle)); |
122 | 0 | continue; |
123 | |
} |
124 | 0 | ServiceReference ref = appContext |
125 | |
.getServiceReference(UrumaService.class.getName()); |
126 | 0 | UrumaServiceImpl service = (UrumaServiceImpl) appContext |
127 | |
.getService(ref); |
128 | 0 | if (service != null |
129 | |
&& UrumaBundleState.getInstance().getAppBundleState() == BundleState.AVAILABLE) { |
130 | 0 | service.registerExtensions(); |
131 | 0 | successedBundleList.add(bundle); |
132 | |
} else { |
133 | 0 | Throwable ex = UrumaBundleState.getInstance() |
134 | |
.getUrumaAppInitializingException(); |
135 | 0 | throw new UrumaAppInitException(bundle, ex, ex.getMessage()); |
136 | |
} |
137 | 0 | } |
138 | 0 | return successedBundleList; |
139 | |
} |
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
protected List<Bundle> findUrumaApplications(final BundleContext context) { |
149 | 0 | Bundle[] bundles = context.getBundles(); |
150 | 0 | List<Bundle> appBundles = new ArrayList<Bundle>(); |
151 | 0 | for (Bundle bundle : bundles) { |
152 | 0 | if (isUrumaApplication(bundle)) { |
153 | 0 | logger.log(URUMA_APPLICATION_FOUND, bundle.getSymbolicName()); |
154 | 0 | appBundles.add(bundle); |
155 | |
} |
156 | |
} |
157 | 0 | return appBundles; |
158 | |
} |
159 | |
|
160 | |
@SuppressWarnings("unchecked") |
161 | |
protected boolean isUrumaApplication(final Bundle bundle) { |
162 | 0 | logger.log(ANALYZING_BUNDLE, bundle.getSymbolicName()); |
163 | 0 | String require = BundleInfoUtil.getHeader(bundle, |
164 | |
BundleInfoUtil.REQUIRE_BUNDLE); |
165 | 0 | if (require != null) { |
166 | 0 | if (require.indexOf(URUMA_BUNDLE_SYMBOLIC_NAME) > 0) { |
167 | 0 | return true; |
168 | |
} |
169 | |
} |
170 | 0 | return false; |
171 | |
} |
172 | |
|
173 | |
protected String[] getBundleSymbolicNames(final List<Bundle> bundles) { |
174 | 0 | String[] names = new String[bundles.size()]; |
175 | 0 | for (int i = 0; i < bundles.size(); i++) { |
176 | 0 | names[i] = bundles.get(i).getSymbolicName(); |
177 | |
} |
178 | 0 | return names; |
179 | |
} |
180 | |
|
181 | |
protected void setupLog4jConfig(final Bundle bundle) { |
182 | 0 | URL xmlConfig = bundle.getResource(DEFAULT_LOG_XML); |
183 | 0 | if (xmlConfig != null) { |
184 | 0 | logger.addXmlConfig(xmlConfig); |
185 | |
} else { |
186 | 0 | URL propConfig = bundle.getResource(DEFAULT_LOG_PROPERTIES); |
187 | 0 | if (propConfig != null) { |
188 | 0 | logger.addPropertyConfig(propConfig); |
189 | |
} |
190 | |
} |
191 | 0 | } |
192 | |
|
193 | |
|
194 | |
|
195 | |
|
196 | |
|
197 | |
|
198 | |
|
199 | |
|
200 | |
|
201 | |
|
202 | |
protected void registerProductPreferenceService(final BundleContext context) { |
203 | 0 | IProductPreferencesService service = new UrumaPreferencesService(); |
204 | 0 | Dictionary<String, Integer> prop = new Hashtable<String, Integer>(); |
205 | 0 | prop.put(Constants.SERVICE_RANKING, 1); |
206 | 0 | context.registerService(IProductPreferencesService.class.getName(), |
207 | |
service, prop); |
208 | 0 | } |
209 | |
} |