1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.seasar.uruma.rcp.util;
17
18 import org.eclipse.core.runtime.Platform;
19 import org.eclipse.ui.internal.util.BundleUtility;
20 import org.osgi.framework.Bundle;
21 import org.osgi.framework.BundleException;
22 import org.seasar.uruma.core.UrumaConstants;
23 import org.seasar.uruma.core.UrumaMessageCodes;
24 import org.seasar.uruma.exception.BundleRuntimeException;
25 import org.seasar.uruma.log.UrumaLogger;
26 import org.seasar.uruma.util.AssertionUtil;
27
28
29
30
31
32
33 public class BundleUtil implements UrumaConstants {
34
35 private static final UrumaLogger logger = UrumaLogger
36 .getLogger(BundleUtil.class);
37
38 private static final String UNINSTALLED = "UNINSTALLED";
39
40 private static final String INSTALLED = "INSTALLED";
41
42 private static final String RESOLVED = "RESOLVED";
43
44 private static final String STARTING = "STARTING";
45
46 private static final String STOPPING = "STOPPING";
47
48 private static final String ACTIVE = "ACTIVE";
49
50
51
52
53
54
55
56
57 public static Bundle getBundle(final String symbolicName) {
58 AssertionUtil.assertNotEmpty("SymbolicName", symbolicName);
59 Bundle bundle = Platform.getBundle(symbolicName);
60 return bundle;
61 }
62
63
64
65
66
67
68
69 public static void start(final String symbolicName) {
70 AssertionUtil.assertNotEmpty("SymbolicName", symbolicName);
71 Bundle targetBundle = getBundle(symbolicName);
72 AssertionUtil.assertNotNull("Bundle", targetBundle);
73 if (!BundleUtility.isActive(targetBundle)) {
74 try {
75 logger.log(UrumaMessageCodes.BUNDLE_START, symbolicName);
76 targetBundle.start();
77 } catch (BundleException e) {
78 throw new BundleRuntimeException(
79 UrumaMessageCodes.EXCEPTION_OCCURED_WITH_REASON, e);
80 }
81 } else {
82 logger.log(UrumaMessageCodes.BUNDLE_STARTED, symbolicName);
83 }
84 }
85
86
87
88
89
90
91 public static void stop(final String symbolicName) {
92 AssertionUtil.assertNotEmpty("SymbolicName", symbolicName);
93 Bundle targetBundle = getBundle(symbolicName);
94 AssertionUtil.assertNotNull("Bundle", targetBundle);
95 if (BundleUtility.isActive(targetBundle)) {
96 try {
97 logger.log(UrumaMessageCodes.BUNDLE_STOP, symbolicName);
98 targetBundle.stop();
99 } catch (BundleException e) {
100 throw new BundleRuntimeException(
101 UrumaMessageCodes.EXCEPTION_OCCURED_WITH_REASON, e);
102 }
103 }
104 }
105
106
107
108
109
110
111
112 public static void update(final String symbolicName) {
113 AssertionUtil.assertNotEmpty("SymbolicName", symbolicName);
114 Bundle targetBundle = getBundle(symbolicName);
115 AssertionUtil.assertNotNull("Bundle", targetBundle);
116 try {
117 logger.log(UrumaMessageCodes.BUNDLE_UPDATE, symbolicName);
118 targetBundle.update();
119 } catch (BundleException e) {
120 throw new BundleRuntimeException(
121 UrumaMessageCodes.EXCEPTION_OCCURED_WITH_REASON, e);
122 }
123 }
124
125
126
127
128
129
130
131
132
133 public static String getBundleState(final Bundle bundle) {
134 if (bundle == null) {
135 return NULL_STRING;
136 }
137
138 switch (bundle.getState()) {
139 case Bundle.ACTIVE:
140 return ACTIVE;
141 case Bundle.INSTALLED:
142 return INSTALLED;
143 case Bundle.RESOLVED:
144 return RESOLVED;
145 case Bundle.STARTING:
146 return STARTING;
147 case Bundle.STOPPING:
148 return STOPPING;
149 case Bundle.UNINSTALLED:
150 return UNINSTALLED;
151 default:
152 return NULL_STRING;
153 }
154
155 }
156 }