Save This Page
Home » org.apache.sling.launchpad.base-2.2.0-source-release » org.apache.sling.launchpad.base.impl » [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;
   20   
   21   import java.io.File;
   22   import java.io.IOException;
   23   import java.io.InputStream;
   24   import java.util.Map;
   25   
   26   import org.apache.felix.framework.Felix;
   27   import org.apache.sling.launchpad.base.shared.Loader;
   28   import org.apache.sling.launchpad.base.shared.Notifiable;
   29   import org.osgi.framework.BundleException;
   30   
   31   
   32   public class SlingFelix extends Felix {
   33   
   34       private final Notifiable notifiable;
   35   
   36       private Notifier notifierThread;
   37   
   38       public SlingFelix(Notifiable notifiable, Map<?, ?> props) throws Exception {
   39           super(props);
   40           this.notifiable = notifiable;
   41       }
   42   
   43       @Override
   44       public void update() throws BundleException {
   45           update(null);
   46       }
   47   
   48       @Override
   49       public void update(InputStream is) throws BundleException {
   50           // get the update file
   51           startNotifier(true, is);
   52   
   53           // just stop the framework now
   54           super.stop();
   55       }
   56   
   57       @Override
   58       public void stop() throws BundleException {
   59           startNotifier(false, null);
   60           super.stop();
   61       }
   62   
   63       public void stop(int status) throws BundleException {
   64           startNotifier(false, null);
   65           super.stop(status);
   66       }
   67   
   68       private synchronized void startNotifier(boolean restart, InputStream ins) {
   69           if (notifierThread == null) {
   70               notifierThread = new Notifier(restart, ins);
   71               notifierThread.setDaemon(false);
   72               notifierThread.start();
   73           }
   74       }
   75   
   76       private class Notifier extends Thread {
   77   
   78           private final boolean restart;
   79   
   80           private final File updateFile;
   81   
   82           private Notifier(boolean restart, InputStream ins) {
   83               super("Sling Notifier");
   84               this.restart = restart;
   85   
   86               if (ins != null) {
   87                   File tmpFile;
   88                   try {
   89                       tmpFile = File.createTempFile("slingupdate", ".jar");
   90                       Loader.spool(ins, tmpFile);
   91                   } catch (IOException ioe) {
   92                       // TOOD: log
   93                       tmpFile = null;
   94                   }
   95                   updateFile = tmpFile;
   96               } else {
   97                   updateFile = null;
   98               }
   99           }
  100   
  101           @Override
  102           public void run() {
  103   
  104               try {
  105                   waitForStop(0);
  106               } catch (InterruptedException ie) {
  107                   // TODO: log
  108               }
  109   
  110               if (restart) {
  111                   notifiable.updated(updateFile);
  112               } else {
  113                   notifiable.stopped();
  114               }
  115           }
  116       }
  117   }

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