Save This Page
Home » org.apache.sling.launchpad.base-2.2.0-source-release » org.apache.sling.launchpad.base.impl.bootstrapcommands » [javadoc | source]
    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one
    3    * or more contributor license agreements.  See the NOTICE file
    4    * distributed with this work for additional information
    5    * regarding copyright ownership.  The ASF licenses this file
    6    * to you under the Apache License, Version 2.0 (the
    7    * "License"); you may not use this file except in compliance
    8    * with the License.  You may obtain a copy of the License at
    9    *
   10    *   http://www.apache.org/licenses/LICENSE-2.0
   11    *
   12    * Unless required by applicable law or agreed to in writing,
   13    * software distributed under the License is distributed on an
   14    * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
   15    * KIND, either express or implied.  See the License for the
   16    * specific language governing permissions and limitations
   17    * under the License.
   18    */
   19   package org.apache.sling.launchpad.base.impl.bootstrapcommands;
   20   
   21   import org.apache.felix.framework.Logger;
   22   import org.apache.felix.framework.util.VersionRange;
   23   import org.osgi.framework.Bundle;
   24   import org.osgi.framework.BundleContext;
   25   
   26   /** A Command that uninstalls a bundle, see
   27    *  {@link UninstallBundleCommandTest} for examples
   28    */
   29   class UninstallBundleCommand implements Command {
   30   
   31       public static String CMD_PREFIX = "uninstall ";
   32       private final String bundleSymbolicName;
   33       private final VersionRange versionRange;
   34   
   35       /** Used to create a prototype object, for parsing */
   36       UninstallBundleCommand() {
   37           bundleSymbolicName = null;
   38           versionRange = null;
   39       }
   40   
   41       /** Used to create an actual command */
   42       private UninstallBundleCommand(String bundleSymbolicName, String versionRangeStr) {
   43           this.bundleSymbolicName = bundleSymbolicName;
   44   
   45           // If versionRangeStr is not a range, make it strict
   46           if(!versionRangeStr.contains(",")) {
   47               versionRangeStr = "[" + versionRangeStr + "," + versionRangeStr + "]";
   48           }
   49           this.versionRange = VersionRange.parse(versionRangeStr);
   50       }
   51   
   52       public void execute(Logger logger, BundleContext ctx) throws Exception {
   53           // Uninstall all instances of our bundle within our version range
   54           for(Bundle b : ctx.getBundles()) {
   55               if(b.getSymbolicName().equals(bundleSymbolicName)) {
   56                   if(versionRange.isInRange(b.getVersion())) {
   57                       logger.log(Logger.LOG_INFO,
   58                               this + ": uninstalling bundle version " + b.getVersion());
   59                       b.uninstall();
   60                   } else {
   61                       logger.log(Logger.LOG_INFO,
   62                               this + ": bundle version (" + b.getVersion()+ " not in range, ignored");
   63                   }
   64               }
   65           }
   66       }
   67   
   68       public Command parse(String commandLine) throws ParseException {
   69           if(commandLine.startsWith(CMD_PREFIX)) {
   70               final String [] s = commandLine.split(" ");
   71               if(s.length == 3) {
   72                   return new UninstallBundleCommand(s[1].trim(), s[2].trim());
   73               }
   74               throw new Command.ParseException("Syntax error: '" + commandLine + "'");
   75           }
   76           return null;
   77       }
   78   
   79       @Override
   80       public String toString() {
   81           return getClass().getSimpleName() + " " + bundleSymbolicName + " " + versionRange;
   82       }
   83   }

Save This Page
Home » org.apache.sling.launchpad.base-2.2.0-source-release » org.apache.sling.launchpad.base.impl.bootstrapcommands » [javadoc | source]