Save This Page
Home » maven-launchpad-plugin-2.0.6-source-release » org.apache.sling.maven.projectsupport » [javadoc | source]
    1   package org.apache.sling.maven.projectsupport;
    2   
    3   import static org.apache.felix.framework.util.FelixConstants.*;
    4   
    5   import java.io.File;
    6   import java.io.IOException;
    7   import java.net.MalformedURLException;
    8   import java.net.URL;
    9   import java.util.ArrayList;
   10   import java.util.Collections;
   11   import java.util.HashMap;
   12   import java.util.Iterator;
   13   import java.util.List;
   14   import java.util.Map;
   15   import java.util.Properties;
   16   
   17   import org.apache.felix.framework.Logger;
   18   import org.apache.maven.artifact.Artifact;
   19   import org.apache.maven.execution.MavenSession;
   20   import org.apache.maven.plugin.MojoExecutionException;
   21   import org.apache.maven.plugin.MojoFailureException;
   22   import org.apache.maven.shared.filtering.MavenFileFilter;
   23   import org.apache.maven.shared.filtering.MavenFilteringException;
   24   import org.apache.maven.shared.filtering.PropertyUtils;
   25   import org.apache.sling.launchpad.base.impl.ResourceProvider;
   26   import org.apache.sling.launchpad.base.impl.Sling;
   27   import org.apache.sling.launchpad.base.shared.Notifiable;
   28   import org.apache.sling.launchpad.base.shared.SharedConstants;
   29   import org.apache.sling.maven.projectsupport.bundlelist.v1_0_0.Bundle;
   30   import org.apache.sling.maven.projectsupport.bundlelist.v1_0_0.BundleList;
   31   import org.apache.sling.maven.projectsupport.bundlelist.v1_0_0.StartLevel;
   32   import org.osgi.framework.BundleException;
   33   
   34   /**
   35    * @author justin
   36    *
   37    */
   38   public abstract class AbstractLaunchpadStartingPlugin extends AbstractBundleListMojo  implements Notifiable {
   39   
   40       /** Default log level setting if no set on command line (value is "INFO"). */
   41       private static final int DEFAULT_LOG_LEVEL = Logger.LOG_INFO;
   42   
   43       /** Mapping between log level numbers and names */
   44       private static final String[] logLevels = { "FATAL", "ERROR", "WARN", "INFO", "DEBUG" };
   45   
   46       /**
   47        * The configuration property setting the port on which the HTTP service
   48        * listens
   49        */
   50       private static final String PROP_PORT = "org.osgi.service.http.port";
   51   
   52       /** Return the log level code for the string */
   53       private static int toLogLevelInt(String level, int defaultLevel) {
   54           for (int i = 0; i < logLevels.length; i++) {
   55               if (logLevels[i].equalsIgnoreCase(level)) {
   56                   return i;
   57               }
   58           }
   59   
   60           return defaultLevel;
   61       }
   62   
   63       /**
   64        * @parameter expression="${http.port}" default-value="8080"
   65        */
   66       private int httpPort;
   67   
   68       /**
   69        * The definition of the package to be included to provide web support for
   70        * JAR-packaged projects (i.e. pax-web).
   71        *
   72        * @parameter
   73        */
   74       private ArtifactDefinition jarWebSupport;
   75   
   76       /**
   77        * @parameter expression="${felix.log.level}"
   78        */
   79       private String logLevel;
   80   
   81       /**
   82        * @parameter expression="${propertiesFile}"
   83        *            default-value="src/test/config/sling.properties"
   84        */
   85       private File propertiesFile;
   86   
   87       /**
   88        * @component
   89        */
   90       private MavenFileFilter mavenFileFilter;
   91   
   92       /**
   93        * @parameter expression="${session}"
   94        * @required
   95        * @readonly
   96        */
   97       private MavenSession mavenSession;
   98   
   99       private ResourceProvider resourceProvider = new ResourceProvider() {
  100   
  101           @Override
  102           public Iterator<String> getChildren(String path) {
  103               if (path.equals("resources/bundles")) {
  104                   List<String> levels = new ArrayList<String>();
  105                   for (StartLevel level : getBundleList().getStartLevels()) {
  106                       levels.add(String.valueOf(level.getLevel()) + "/");
  107                   }
  108                   return levels.iterator();
  109               } else if (path.equals("resources/corebundles")) {
  110                   List<String> empty = Collections.emptyList();
  111                   return empty.iterator();
  112               } else {
  113                   try {
  114                       int i = Integer.parseInt(path);
  115                       List<String> bundles = new ArrayList<String>();
  116                       for (StartLevel level : getBundleList().getStartLevels()) {
  117                           if (level.getLevel() == i) {
  118                               for (Bundle bundle : level.getBundles()) {
  119                                   ArtifactDefinition d = new ArtifactDefinition(bundle, i);
  120                                   try {
  121                                       Artifact artifact = getArtifact(d);
  122                                       bundles.add(artifact.getFile().toURI().toURL().toExternalForm());
  123                                   } catch (Exception e) {
  124                                       getLog().error("Unable to resolve artifact ", e);
  125                                   }
  126                               }
  127   
  128                               break;
  129                           }
  130                       }
  131                       return bundles.iterator();
  132   
  133                   } catch (NumberFormatException e) {
  134                       getLog().warn("un-handlable path " + path);
  135                       return null;
  136   
  137                   }
  138               }
  139           }
  140   
  141           @Override
  142           public URL getResource(String path) {
  143               if (path.endsWith(".properties")) {
  144                   return getClass().getResource("/" + path);
  145               } else {
  146                   try {
  147                       return new URL(path);
  148                   } catch (MalformedURLException e) {
  149                       getLog().error("Expecting a real URL", e);
  150                       return null;
  151                   }
  152               }
  153           }
  154       };
  155   
  156       private Sling sling;
  157   
  158       /**
  159        * @parameter expression="${sling.home}" default-value="sling"
  160        */
  161       private String slingHome;
  162   
  163       /**
  164        * @parameter default-value="true"
  165        */
  166       private boolean forceBundleLoad;
  167   
  168       public void stopped() {
  169           sling = null;
  170       }
  171   
  172       public void updated(File tmpFile) {
  173           // TODO - should anything happen here?
  174           getLog().info("File updated " + tmpFile.getAbsolutePath());
  175       }
  176   
  177       /**
  178        * {@inheritDoc}
  179        */
  180       @Override
  181       protected void executeWithArtifacts() throws MojoExecutionException, MojoFailureException {
  182           try {
  183               final Map<String, String> props = new HashMap<String, String>();
  184   
  185               props.put(SharedConstants.SLING_HOME, slingHome);
  186   
  187               if (forceBundleLoad) {
  188                   props.put(SharedConstants.FORCE_PACKAGE_BUNDLE_LOADING, "true");
  189               }
  190   
  191               // set up and configure Felix Logger
  192               int logLevelNum;
  193               if (logLevel == null) {
  194                   logLevelNum = DEFAULT_LOG_LEVEL;
  195               } else {
  196                   logLevelNum = toLogLevelInt(logLevel, DEFAULT_LOG_LEVEL);
  197               }
  198               props.put(LOG_LEVEL_PROP, String.valueOf(logLevelNum));
  199               // Display port number on console, in case HttpService doesn't
  200               getLog().info("HTTP server port: " + httpPort);
  201               props.put(PROP_PORT, String.valueOf(httpPort));
  202   
  203               // prevent tons of needless WARN from the framework
  204               Logger logger = new Logger();
  205               logger.setLogLevel(Logger.LOG_ERROR);
  206   
  207               if (propertiesFile.exists()) {
  208                   File tmp = null;
  209                   try {
  210                       tmp = File.createTempFile("sling", "props");
  211                       mavenFileFilter.copyFile(propertiesFile, tmp, true, project, null, true, System
  212                               .getProperty("file.encoding"), mavenSession);
  213                       Properties loadedProps = PropertyUtils.loadPropertyFile(tmp, null);
  214                       for (Object key : loadedProps.keySet()) {
  215                           props.put((String) key, (String) loadedProps.get(key));
  216                       }
  217                   } catch (IOException e) {
  218                       throw new MojoExecutionException("Unable to create filtered properties file", e);
  219                   } catch (MavenFilteringException e) {
  220                       throw new MojoExecutionException("Unable to create filtered properties file", e);
  221                   } finally {
  222                       if (tmp != null) {
  223                           tmp.delete();
  224                       }
  225                   }
  226               }
  227   
  228               sling = startSling(resourceProvider, props, logger);
  229   
  230           } catch (BundleException be) {
  231               getLog().error("Failed to Start OSGi framework", be);
  232           }
  233   
  234       }
  235   
  236       protected abstract Sling startSling(ResourceProvider resourceProvider, Map<String, String> props, Logger logger) throws BundleException;
  237   
  238       protected void stopSling() {
  239           if (sling != null) {
  240               sling.destroy();
  241           }
  242       }
  243   
  244       protected void initArtifactDefinitions(Properties dependencies) {
  245           if (jarWebSupport == null) {
  246               jarWebSupport = new ArtifactDefinition();
  247           }
  248           jarWebSupport.initDefaults(dependencies.getProperty("jarWebSupport"));
  249       }
  250   
  251       /**
  252        * Add the JAR Web Support bundle to the bundle list.
  253        */
  254       @Override
  255       protected void initBundleList(BundleList bundleList) {
  256           bundleList.add(jarWebSupport.toBundle());
  257       }
  258   }

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