Save This Page
Home » maven-launchpad-plugin-2.0.6-source-release » org.apache.sling.maven.projectsupport » [javadoc | source]
    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements. See the NOTICE file distributed with this
    4    * work for additional information regarding copyright ownership. The ASF
    5    * licenses this file to You under the Apache License, Version 2.0 (the
    6    * "License"); you may not use this file except in compliance with the License.
    7    * You may obtain a copy of the License at
    8    *
    9    * http://www.apache.org/licenses/LICENSE-2.0
   10    *
   11    * Unless required by applicable law or agreed to in writing, software
   12    * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
   13    * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
   14    * License for the specific language governing permissions and limitations under
   15    * the License.
   16    */
   17   package org.apache.sling.maven.projectsupport;
   18   
   19   import java.util.Map;
   20   
   21   import org.apache.felix.framework.Logger;
   22   import org.apache.sling.launchpad.base.impl.ResourceProvider;
   23   import org.apache.sling.launchpad.base.impl.Sling;
   24   import org.osgi.framework.BundleException;
   25   
   26   /**
   27    * Run a Launchpad application.
   28    *
   29    * @goal run
   30    * @requiresDependencyResolution test
   31    *
   32    */
   33   public class RunMojo extends AbstractLaunchpadStartingPlugin {
   34   
   35   
   36   
   37       private Thread shutdown = new Thread() {
   38           /**
   39            * Called when the Java VM is being terminiated, for example because the
   40            * KILL signal has been sent to the process. This method calls stop on
   41            * the launched Sling instance to terminate the framework before
   42            * returning.
   43            */
   44           @Override
   45           public void run() {
   46               getLog().info("Java VM is shutting down", null);
   47               shutdown();
   48           }
   49   
   50           // ---------- Shutdown support for control listener and shutdown hook
   51   
   52           void shutdown() {
   53               // remove the shutdown hook, will fail if called from the
   54               // shutdown hook itself. Otherwise this prevents shutdown
   55               // from being called again
   56               try {
   57                   Runtime.getRuntime().removeShutdownHook(this);
   58               } catch (Throwable t) {
   59                   // don't care for problems removing the hook
   60               }
   61   
   62               stopSling();
   63           }
   64       };
   65   
   66       protected Sling startSling(ResourceProvider resourceProvider, final Map<String, String> props, Logger logger)
   67               throws BundleException {
   68           Runtime.getRuntime().addShutdownHook(shutdown);
   69   
   70           // creating the instance launches the framework and we are done here
   71           Sling mySling = new Sling(this, logger, resourceProvider, props) {
   72   
   73               // overwrite the loadPropertiesOverride method to inject the
   74               // mojo arguments unconditionally. These will not be persisted
   75               // in any properties file, though
   76               protected void loadPropertiesOverride(
   77                       Map<String, String> properties) {
   78                   if (props != null) {
   79                       properties.putAll(props);
   80                   }
   81               }
   82           };
   83   
   84           // TODO this seems hacky!
   85           try {
   86               while (mySling != null) {
   87                   Thread.sleep(100);
   88               }
   89           } catch (InterruptedException e) {
   90           }
   91   
   92           return mySling;
   93       }
   94   
   95   
   96   }

Save This Page
Home » maven-launchpad-plugin-2.0.6-source-release » org.apache.sling.maven.projectsupport » [javadoc | source]