View Javadoc

1   /*
2    * Copyright 2004-2008 the Seasar Foundation and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
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   * {@link Bundle} を扱うためのユーティリティクラスです。
30   * 
31   * @author y.sugigami
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       * {@link Bundle}を取得します。<br />
52       * 
53       * @param symbolicName
54       *            pluginID
55       * @return {@link Bundle} オブジェクト
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       * {@link Bundle}を開始します。<br />
65       * 
66       * @param symbolicName
67       *            pluginID
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       * {@link Bundle}を停止します。<br />
88       * 
89       * @param symbolicName
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      * {@link Bundle}を更新します。<br />
108      * 
109      * @param symbolicName
110      *            pluginID
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      * バンドルの状態を表す文字列を返します。<br />
127      * 
128      * @param bundle
129      *            {@link Bundle} オブジェクト
130      * @return バンドルの状態を表す文字列。<code>bundle</code> が <code>null</code>
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 }