You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@aries.apache.org by ej...@apache.org on 2011/04/07 22:19:24 UTC

svn commit: r1089983 - in /aries/trunk/application/application-tooling-repository-generator: ./ src/ src/main/ src/main/java/ src/main/java/org/ src/main/java/org/apache/ src/main/java/org/apache/aries/ src/main/java/org/apache/aries/application/ src/m...

Author: ejiang
Date: Thu Apr  7 20:19:24 2011
New Revision: 1089983

URL: http://svn.apache.org/viewvc?rev=1089983&view=rev
Log:
ARIES-618: Create a command line repository generator tool

Added:
    aries/trunk/application/application-tooling-repository-generator/
    aries/trunk/application/application-tooling-repository-generator/pom.xml
    aries/trunk/application/application-tooling-repository-generator/src/
    aries/trunk/application/application-tooling-repository-generator/src/main/
    aries/trunk/application/application-tooling-repository-generator/src/main/java/
    aries/trunk/application/application-tooling-repository-generator/src/main/java/org/
    aries/trunk/application/application-tooling-repository-generator/src/main/java/org/apache/
    aries/trunk/application/application-tooling-repository-generator/src/main/java/org/apache/aries/
    aries/trunk/application/application-tooling-repository-generator/src/main/java/org/apache/aries/application/
    aries/trunk/application/application-tooling-repository-generator/src/main/java/org/apache/aries/application/repository/
    aries/trunk/application/application-tooling-repository-generator/src/main/java/org/apache/aries/application/repository/generator/
    aries/trunk/application/application-tooling-repository-generator/src/main/java/org/apache/aries/application/repository/generator/AriesRepositoryGenerator.java

Added: aries/trunk/application/application-tooling-repository-generator/pom.xml
URL: http://svn.apache.org/viewvc/aries/trunk/application/application-tooling-repository-generator/pom.xml?rev=1089983&view=auto
==============================================================================
--- aries/trunk/application/application-tooling-repository-generator/pom.xml (added)
+++ aries/trunk/application/application-tooling-repository-generator/pom.xml Thu Apr  7 20:19:24 2011
@@ -0,0 +1,167 @@
+<?xml version="1.0" encoding="UTF-8"?>
+    <!--
+        Licensed to the Apache Software Foundation (ASF) under one or
+        more contributor license agreements. See the NOTICE file
+        distributed with this work for additional information regarding
+        copyright ownership. The ASF licenses this file to you under the
+        Apache License, Version 2.0 (the "License"); you may not use
+        this file except in compliance with the License. You may obtain
+        a copy of the License at
+
+        http://www.apache.org/licenses/LICENSE-2.0 Unless required by
+        applicable law or agreed to in writing, software distributed
+        under the License is distributed on an "AS IS" BASIS, WITHOUT
+        WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+        See the License for the specific language governing permissions
+        and limitations under the License.
+    -->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+
+    <modelVersion>4.0.0</modelVersion>
+    <parent>
+        <groupId>org.apache.aries</groupId>
+        <artifactId>java6-parent</artifactId>
+        <version>0.4</version>
+        <relativePath />
+    </parent>
+    <groupId>org.apache.aries.application</groupId>
+    <artifactId>org.apache.aries.application.tooling.repository.generator</artifactId>
+    <packaging>bundle</packaging>
+    <version>0.1.0-SNAPSHOT</version>
+    <name>Apache Aries repository generator tool</name>
+
+    <scm>
+         <connection>scm:svn:http://svn.apache.org/repos/asf/aries/trunk/application/application-tooling-repository-generator</connection>
+         <developerConnection>scm:svn:https://svn.apache.org/repos/asf/aries/trunk/application/application-tooling-repository-generator</developerConnection>
+         <url>http://svn.apache.org/viewvc/aries/trunk/application/application-tooling-repository-generator</url>
+     </scm>
+    <properties>
+        <aries.osgi.export.pkg>
+        </aries.osgi.export.pkg>
+        <aries.osgi.private.pkg>
+            org.apache.aries.application.repository.generator
+        </aries.osgi.private.pkg>
+    </properties>
+    <dependencies>
+        <dependency>
+            <groupId>org.eclipse</groupId>
+            <artifactId>osgi</artifactId>
+        </dependency>
+
+        <dependency>
+            <groupId>org.eclipse.equinox</groupId>
+            <artifactId>cm</artifactId>
+        </dependency>
+
+        <dependency>
+            <groupId>org.eclipse.osgi</groupId>
+            <artifactId>services</artifactId>
+        </dependency>
+
+        <dependency>
+            <groupId>org.ops4j.pax.logging</groupId>
+            <artifactId>pax-logging-api</artifactId>
+        </dependency>
+
+        <dependency>
+            <groupId>org.ops4j.pax.logging</groupId>
+            <artifactId>pax-logging-service</artifactId>
+        </dependency>
+
+        <dependency>
+            <groupId>org.apache.felix</groupId>
+            <artifactId>org.apache.felix.bundlerepository</artifactId>
+            <version>1.6.4</version>
+        </dependency>
+
+        <dependency>
+            <groupId>org.apache.aries.blueprint</groupId>
+            <artifactId>org.apache.aries.blueprint</artifactId>
+            <version>0.3.2-SNAPSHOT</version>
+        </dependency>
+
+        <dependency>
+            <groupId>org.apache.aries.proxy</groupId>
+            <artifactId>org.apache.aries.proxy</artifactId>
+            <version>0.3.1-SNAPSHOT</version>
+        </dependency>
+
+        <dependency>
+            <groupId>org.apache.aries</groupId>
+            <artifactId>org.apache.aries.util</artifactId>
+            <version>0.4-SNAPSHOT</version>
+        </dependency>
+
+        <dependency>
+            <groupId>org.apache.aries.application</groupId>
+            <artifactId>org.apache.aries.application.api</artifactId>
+            <version>0.3.1-SNAPSHOT</version>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.aries.application</groupId>
+            <artifactId>org.apache.aries.application.utils</artifactId>
+            <version>0.3.1-SNAPSHOT</version>
+        </dependency>
+
+        <dependency>
+            <groupId>org.apache.aries.application</groupId>
+            <artifactId>org.apache.aries.application.default.local.platform</artifactId>
+            <version>0.3.1-SNAPSHOT</version>
+        </dependency>
+
+        <dependency>
+            <groupId>org.apache.aries.application</groupId>
+            <artifactId>org.apache.aries.application.modeller</artifactId>
+            <version>0.3.1-SNAPSHOT</version>
+        </dependency>
+
+        <dependency>
+            <groupId>org.apache.aries.application</groupId>
+            <artifactId>org.apache.aries.application.resolver.obr</artifactId>
+            <version>0.3.1-SNAPSHOT</version>
+        </dependency>
+
+        <dependency>
+            <groupId>asm</groupId>
+            <artifactId>asm-all</artifactId>
+            <version>3.2</version>
+        </dependency>
+    </dependencies>
+
+    <description>Command line tool for generating repository xml</description>
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-jar-plugin</artifactId>
+                <configuration>
+                    <archive>
+                        <manifest>
+                            <mainClass>org.apache.aries.application.repository.generator.AriesRepositoryGenerator</mainClass>
+                            <addClasspath>true</addClasspath>
+                        </manifest>
+                    </archive>
+                </configuration>
+            </plugin>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-dependency-plugin</artifactId>
+                <executions>
+                    <execution>
+                        <id>copy</id>
+                        <phase>package</phase>
+                        <goals>
+                            <goal>copy-dependencies</goal>
+                        </goals>
+                        <configuration>
+                            <excludeTransitive>true</excludeTransitive>
+                            <outputDirectory>${project.build.directory}</outputDirectory>
+                        </configuration>
+                    </execution>
+                </executions>
+            </plugin>
+        </plugins>
+    </build>
+
+</project>

Added: aries/trunk/application/application-tooling-repository-generator/src/main/java/org/apache/aries/application/repository/generator/AriesRepositoryGenerator.java
URL: http://svn.apache.org/viewvc/aries/trunk/application/application-tooling-repository-generator/src/main/java/org/apache/aries/application/repository/generator/AriesRepositoryGenerator.java?rev=1089983&view=auto
==============================================================================
--- aries/trunk/application/application-tooling-repository-generator/src/main/java/org/apache/aries/application/repository/generator/AriesRepositoryGenerator.java (added)
+++ aries/trunk/application/application-tooling-repository-generator/src/main/java/org/apache/aries/application/repository/generator/AriesRepositoryGenerator.java Thu Apr  7 20:19:24 2011
@@ -0,0 +1,247 @@
+package org.apache.aries.application.repository.generator;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.lang.reflect.Method;
+import java.net.URLDecoder;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.List;
+import java.util.ServiceConfigurationError;
+import java.util.ServiceLoader;
+
+import org.apache.aries.application.management.spi.repository.RepositoryGenerator;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.BundleException;
+import org.osgi.framework.Constants;
+import org.osgi.framework.Filter;
+import org.osgi.framework.FrameworkUtil;
+import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.framework.ServiceReference;
+import org.osgi.framework.launch.Framework;
+import org.osgi.framework.launch.FrameworkFactory;
+import org.osgi.service.packageadmin.PackageAdmin;
+import org.osgi.util.tracker.ServiceTracker;
+
+public class AriesRepositoryGenerator {
+
+  private Framework framework = null;
+  private List<String> ignoreList = new ArrayList<String>();
+  private List<ServiceTracker> srs = new ArrayList<ServiceTracker>();
+  public static final long DEFAULT_TIMEOUT = 60000; 
+  public static final String ERROR_LEVEL = "ERROR";
+  public static final String DEFAULT_REPO_NAME="reporsitory.xml";
+
+  /**
+   * Start OSGi framework and install the necessary bundles
+   * @return
+   * @throws BundleException
+   */
+  public BundleContext startFramework() throws BundleException{
+    ServiceLoader<FrameworkFactory> factoryLoader =
+      ServiceLoader.load(FrameworkFactory.class, getClass().getClassLoader());
+
+    Iterator<FrameworkFactory> factoryIterator = factoryLoader.iterator();
+    //Map<String, String> osgiPropertyMap = new HashMap<String, String>();
+    if (!!!factoryIterator.hasNext()) {
+      System.out.println( "Unable to locate the osgi jar");
+    }
+    
+    try {
+      FrameworkFactory frameworkFactory = factoryIterator.next();
+      framework = frameworkFactory.newFramework(Collections.EMPTY_MAP);
+    } catch (ServiceConfigurationError sce) {
+      sce.printStackTrace();
+    }
+    framework.init();
+    framework.start();
+    // install the bundles in the current directory
+    Collection<Bundle> installedBundles = new ArrayList<Bundle>();
+    File bundleDir = new File(".");
+    File[] jars = bundleDir.listFiles();
+    try {
+      for (File jar : jars) {
+        if (jar.isFile() && (jar.getName().endsWith(".jar"))) {
+          String location = URLDecoder.decode(jar.toURI().toURL().toExternalForm(), "UTF-8");
+          if (shouldInstall(location, getIgnoreList())) {
+            installedBundles.add(framework.getBundleContext().installBundle(
+                "reference:" + location));
+          }
+        }
+      }
+    } catch (Exception e) {
+      e.printStackTrace();
+    }
+    
+    ServiceReference paRef = framework.getBundleContext().getServiceReference(PackageAdmin.class.getCanonicalName());
+    if (paRef != null) {
+      try {
+        PackageAdmin admin = (PackageAdmin) framework.getBundleContext().getService(paRef);
+        admin.resolveBundles(installedBundles.toArray(new Bundle[installedBundles.size()]));
+      } finally {
+        framework.getBundleContext().ungetService(paRef);
+      }
+    } else {
+      System.out.println("Unable to find the service reference for package admin");
+    }
+
+
+    //start the bundles
+    //loop through the list of installed bundles to start them
+    for (Bundle bundle : installedBundles) {
+      try {
+        if (bundle.getHeaders().get(Constants.FRAGMENT_HOST) == null) {
+          //start the bundle using its activiation policy
+          bundle.start(Bundle.START_ACTIVATION_POLICY);
+        } else {
+          //nothing to start, we have got a fragment
+        }
+      } catch (BundleException e) {
+        e.printStackTrace();
+        continue;
+      }
+    }
+    return framework.getBundleContext();
+  }
+
+  private  void stopFramework() throws Exception{
+    for (ServiceTracker st : srs) {
+      if (st != null) {
+        st.close();
+      }  
+    }
+    if (framework != null) {
+      framework.stop();
+    }
+  }
+  private Object getOsgiService(BundleContext bc, String className) {
+    ServiceTracker tracker = null;
+    try {
+      String flt = "(" + Constants.OBJECTCLASS + "=" + className + ")";
+      Filter osgiFilter = FrameworkUtil.createFilter(flt);
+      tracker = new ServiceTracker(bc, osgiFilter, null);
+      tracker.open();
+      // add tracker to the list of trackers we close at tear down
+      srs.add(tracker);
+      Object x = tracker.waitForService(DEFAULT_TIMEOUT);
+      if (x == null) {
+        throw new RuntimeException("Gave up waiting for service " + flt);
+      }
+      return x;
+    } catch (InvalidSyntaxException e) {
+      throw new IllegalArgumentException("Invalid filter", e);
+    } catch (InterruptedException e) {
+      throw new RuntimeException(e);
+    }
+  }
+  
+  public static void usage() {
+    System.out.println("Invalid parameter specifed. See program usage below.");
+    System.out.println("========================= Usage ===============================");
+    System.out.println("Parameter list:  [Reporsitory File Location] url1 [url2 url3 ...]");
+    System.out.println();
+    System.out.println("The parameter of the repository file location is the location for the genenerated reporsitory xml, e.g. /test/rep/repo.xml. It must end with .xml. If the parameter is not present, it will generate a repository.xml in the current directory.");
+    System.out.println();
+    System.out.println("The paremater of url1 [url2 url 3 ...] is a list of urls. If the url starts with file:, it can be a directory, which means all jar or war files in that directory to be included in the reposiotry.");
+    System.out.println("===============================================================");
+  }
+  /**
+   * Execute this program using 
+   * java -jar thisbundlename arg0 arg1...
+   * arg0 can be the location of the repository xml or a url
+   * arg1... a list of url. If there is a file url, all jar/wars under the url are to be included.
+   * e.g. file:///c:/temp/, all jars/wars under temp or its subdirectory are to be included.
+   * @param args
+   */
+  public static void main(String[] args) {
+    String[] urlArray = args;
+    if (args.length == 0){
+      usage();
+      System.exit(0);
+    } else {
+      AriesRepositoryGenerator generator = new AriesRepositoryGenerator();
+      
+      // set the system property for logging if not set in the command line
+      String loggerLevelProp = "org.ops4j.pax.logging.DefaultServiceLog.level";
+      if (System.getProperty(loggerLevelProp) == null) {
+        System.setProperty(loggerLevelProp, ERROR_LEVEL);
+      }
+      FileOutputStream fout = null;
+      try {
+        BundleContext ctx = generator.startFramework();
+        // get the object of repositoryGenerator and call its method
+        
+        File xmlFile = new File(DEFAULT_REPO_NAME);
+        if (args[0].endsWith(".xml")) {
+           xmlFile = new File(args[0]);
+          // get the directors
+          File parentDir = xmlFile.getAbsoluteFile().getParentFile();
+          if (!!!parentDir.exists()) {
+            parentDir.mkdirs();
+          }
+
+          // put the rest of the args to the list of urls
+          if (args.length >1) {
+            urlArray = Arrays.copyOfRange(args, 1, args.length);
+          } else {
+            usage();
+            System.exit(0);
+          }
+        } 
+        // Use reflection to get around the class loading issue
+        fout = new FileOutputStream(xmlFile);
+        Object repoGen = generator.getOsgiService(ctx, RepositoryGenerator.class.getName());
+        Class gen= repoGen.getClass();
+        Method m = gen.getDeclaredMethod("generateRepository", new Class[]{String[].class, OutputStream.class});
+        m.invoke(repoGen, urlArray, fout);
+        generator.stopFramework();
+        System.out.println("The reporsitory xml was generated successfully under " + xmlFile.getAbsolutePath() + ".");
+
+      } catch (Exception e) {
+        e.printStackTrace();
+        System.exit(0);
+      } finally {
+        try {
+          if (fout != null)
+            fout.close();
+        }
+        catch (IOException e) {
+          fout = null;
+        }
+      }
+    }
+  }
+
+  /**
+   * Return whether to install the specified bundle
+   * @param location bundle location
+   * @param ignoreList the ignore list containing the bundles to be ignored
+   * @return
+   */
+  private boolean shouldInstall(String location, List<String> ignoreList)
+  {
+    String name = location.substring(location.lastIndexOf('/') + 1);
+    //check that it isn't one of the files we should ignore
+    boolean inIgnoreList = false;
+    for (String prefix : ignoreList) {
+      //we check for _ in case of version and .jar in case of a prefix.*.jar we should allow
+      if (name.startsWith(prefix + "-") || name.startsWith(prefix + ".jar")) {
+        inIgnoreList = true;
+        break;
+      }
+    }
+    return !!!inIgnoreList;
+  }
+
+  private List<String> getIgnoreList() {
+    ignoreList.add("osgi");
+    ignoreList.add("org.apache.aries.application.obr.generator");
+    return ignoreList;
+  }
+}