Coverage Report - org.seasar.uruma.rcp.util.BundleUtil
 
Classes in this File Line Coverage Branch Coverage Complexity
BundleUtil
0%
0/45
0%
0/13
5.4
 
 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  0
 public class BundleUtil implements UrumaConstants {
 34  
 
 35  0
     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  0
         AssertionUtil.assertNotEmpty("SymbolicName", symbolicName);
 59  0
         Bundle bundle = Platform.getBundle(symbolicName);
 60  0
         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  0
         AssertionUtil.assertNotEmpty("SymbolicName", symbolicName);
 71  0
         Bundle targetBundle = getBundle(symbolicName);
 72  0
         AssertionUtil.assertNotNull("Bundle", targetBundle);
 73  0
         if (!BundleUtility.isActive(targetBundle)) {
 74  
             try {
 75  0
                 logger.log(UrumaMessageCodes.BUNDLE_START, symbolicName);
 76  0
                 targetBundle.start();
 77  0
             } catch (BundleException e) {
 78  0
                 throw new BundleRuntimeException(
 79  
                         UrumaMessageCodes.EXCEPTION_OCCURED_WITH_REASON, e);
 80  0
             }
 81  
         } else {
 82  0
             logger.log(UrumaMessageCodes.BUNDLE_STARTED, symbolicName);
 83  
         }
 84  0
     }
 85  
 
 86  
     /**
 87  
      * {@link Bundle}を停止します。<br />
 88  
      * 
 89  
      * @param symbolicName
 90  
      */
 91  
     public static void stop(final String symbolicName) {
 92  0
         AssertionUtil.assertNotEmpty("SymbolicName", symbolicName);
 93  0
         Bundle targetBundle = getBundle(symbolicName);
 94  0
         AssertionUtil.assertNotNull("Bundle", targetBundle);
 95  0
         if (BundleUtility.isActive(targetBundle)) {
 96  
             try {
 97  0
                 logger.log(UrumaMessageCodes.BUNDLE_STOP, symbolicName);
 98  0
                 targetBundle.stop();
 99  0
             } catch (BundleException e) {
 100  0
                 throw new BundleRuntimeException(
 101  
                         UrumaMessageCodes.EXCEPTION_OCCURED_WITH_REASON, e);
 102  0
             }
 103  
         }
 104  0
     }
 105  
 
 106  
     /**
 107  
      * {@link Bundle}を更新します。<br />
 108  
      * 
 109  
      * @param symbolicName
 110  
      *            pluginID
 111  
      */
 112  
     public static void update(final String symbolicName) {
 113  0
         AssertionUtil.assertNotEmpty("SymbolicName", symbolicName);
 114  0
         Bundle targetBundle = getBundle(symbolicName);
 115  0
         AssertionUtil.assertNotNull("Bundle", targetBundle);
 116  
         try {
 117  0
             logger.log(UrumaMessageCodes.BUNDLE_UPDATE, symbolicName);
 118  0
             targetBundle.update();
 119  0
         } catch (BundleException e) {
 120  0
             throw new BundleRuntimeException(
 121  
                     UrumaMessageCodes.EXCEPTION_OCCURED_WITH_REASON, e);
 122  0
         }
 123  0
     }
 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  0
         if (bundle == null) {
 135  0
             return NULL_STRING;
 136  
         }
 137  
 
 138  0
         switch (bundle.getState()) {
 139  
         case Bundle.ACTIVE:
 140  0
             return ACTIVE;
 141  
         case Bundle.INSTALLED:
 142  0
             return INSTALLED;
 143  
         case Bundle.RESOLVED:
 144  0
             return RESOLVED;
 145  
         case Bundle.STARTING:
 146  0
             return STARTING;
 147  
         case Bundle.STOPPING:
 148  0
             return STOPPING;
 149  
         case Bundle.UNINSTALLED:
 150  0
             return UNINSTALLED;
 151  
         default:
 152  0
             return NULL_STRING;
 153  
         }
 154  
 
 155  
     }
 156  
 }