Repository: karaf
Updated Branches:
  refs/heads/karaf-2.x d17789428 -> 4b310a8e7


[KARAF-3240] Remove pax-url-mvn dependency in the BundleWatcher


Project: http://git-wip-us.apache.org/repos/asf/karaf/repo
Commit: http://git-wip-us.apache.org/repos/asf/karaf/commit/4b310a8e
Tree: http://git-wip-us.apache.org/repos/asf/karaf/tree/4b310a8e
Diff: http://git-wip-us.apache.org/repos/asf/karaf/diff/4b310a8e

Branch: refs/heads/karaf-2.x
Commit: 4b310a8e7a6c67a83f1a615e5fc03939b29d3467
Parents: d177894
Author: Jean-Baptiste Onofré <[email protected]>
Authored: Wed Dec 24 15:55:28 2014 +0100
Committer: Jean-Baptiste Onofré <[email protected]>
Committed: Wed Dec 24 15:55:28 2014 +0100

----------------------------------------------------------------------
 pom.xml                                         |   5 -
 shell/dev/pom.xml                               |  13 +-
 .../karaf/shell/dev/watch/BundleWatcher.java    | 102 +++--
 .../org/apache/karaf/util/maven/Parser.java     | 408 +++++++++++++++++++
 4 files changed, 474 insertions(+), 54 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/karaf/blob/4b310a8e/pom.xml
----------------------------------------------------------------------
diff --git a/pom.xml b/pom.xml
index bdee70e..fc549e8 100644
--- a/pom.xml
+++ b/pom.xml
@@ -1025,11 +1025,6 @@
                 <version>${pax.url.version}</version>
             </dependency>
             <dependency>
-                <groupId>org.ops4j.pax.url</groupId>
-                <artifactId>pax-url-mvn</artifactId>
-                <version>1.3.7</version>
-            </dependency>
-            <dependency>
                 <groupId>org.apache.geronimo.specs</groupId>
                 <artifactId>geronimo-jms_1.1_spec</artifactId>
                 <version>${geronimo.jms-spec.version}</version>

http://git-wip-us.apache.org/repos/asf/karaf/blob/4b310a8e/shell/dev/pom.xml
----------------------------------------------------------------------
diff --git a/shell/dev/pom.xml b/shell/dev/pom.xml
index 715f54c..b301757 100644
--- a/shell/dev/pom.xml
+++ b/shell/dev/pom.xml
@@ -46,12 +46,6 @@
         </dependency>
 
         <dependency>
-            <groupId>org.ops4j.pax.url</groupId>
-            <artifactId>pax-url-mvn</artifactId>
-            <scope>provided</scope>
-        </dependency>
-
-        <dependency>
             <groupId>org.osgi</groupId>
             <artifactId>org.osgi.core</artifactId>
             <scope>provided</scope>
@@ -74,6 +68,11 @@
             <artifactId>org.apache.felix.utils</artifactId>
             <scope>provided</scope>
         </dependency>
+        <dependency>
+            <groupId>org.apache.karaf</groupId>
+            <artifactId>org.apache.karaf.util</artifactId>
+            <scope>provided</scope>
+        </dependency>
     </dependencies>
 
     <build>
@@ -98,7 +97,6 @@
                 <artifactId>maven-bundle-plugin</artifactId>
                 <configuration>
                     <instructions>
-                        <Embed-Dependency>pax-url-mvn</Embed-Dependency>
                         <Import-Package>
                             org.apache.felix.service.command,
                             org.apache.felix.gogo.commands,
@@ -107,6 +105,7 @@
                         </Import-Package>
                         <Private-Package>
                             
org.apache.felix.utils*;-split-package:=merge-first,
+                            
org.apache.karaf.util.maven;-split-package:=merge-first,
                             org.ops4j.pax.url.mvn
                         </Private-Package>
                     </instructions>

http://git-wip-us.apache.org/repos/asf/karaf/blob/4b310a8e/shell/dev/src/main/java/org/apache/karaf/shell/dev/watch/BundleWatcher.java
----------------------------------------------------------------------
diff --git 
a/shell/dev/src/main/java/org/apache/karaf/shell/dev/watch/BundleWatcher.java 
b/shell/dev/src/main/java/org/apache/karaf/shell/dev/watch/BundleWatcher.java
index f254f84..202bd1a 100644
--- 
a/shell/dev/src/main/java/org/apache/karaf/shell/dev/watch/BundleWatcher.java
+++ 
b/shell/dev/src/main/java/org/apache/karaf/shell/dev/watch/BundleWatcher.java
@@ -16,10 +16,7 @@
  */
 package org.apache.karaf.shell.dev.watch;
 
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.IOException;
-import java.io.InputStream;
+import java.io.*;
 import java.net.MalformedURLException;
 import java.util.ArrayList;
 import java.util.Dictionary;
@@ -31,11 +28,7 @@ import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.concurrent.atomic.AtomicInteger;
 
-import org.ops4j.pax.url.maven.commons.MavenConfiguration;
-import org.ops4j.pax.url.maven.commons.MavenConfigurationImpl;
-import org.ops4j.pax.url.maven.commons.MavenRepositoryURL;
-import org.ops4j.pax.url.mvn.ServiceConstants;
-import org.ops4j.pax.url.mvn.internal.Parser;
+import org.apache.karaf.util.maven.Parser;
 import org.ops4j.util.property.DictionaryPropertyResolver;
 import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleContext;
@@ -44,14 +37,17 @@ import org.osgi.framework.BundleException;
 import org.osgi.framework.BundleListener;
 import org.osgi.framework.FrameworkEvent;
 import org.osgi.framework.FrameworkListener;
-import org.osgi.framework.ServiceReference;
 import org.osgi.framework.wiring.FrameworkWiring;
 import org.osgi.service.cm.Configuration;
 import org.osgi.service.cm.ConfigurationAdmin;
-import org.osgi.service.packageadmin.PackageAdmin;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import javax.xml.stream.XMLInputFactory;
+import javax.xml.stream.XMLStreamConstants;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamReader;
+
 /**
  * A Runnable singleton which watches at the defined location for bundle 
updates.
  */
@@ -187,37 +183,6 @@ public class BundleWatcher implements Runnable, 
BundleListener {
         return null;
     }
 
-    public File getLocalRepository() {
-        // Attempt to retrieve local repository location from 
MavenConfiguration
-        MavenConfiguration configuration = retrieveMavenConfiguration();
-        if (configuration != null) {
-            MavenRepositoryURL localRepositoryURL = 
configuration.getLocalRepository();
-            if (localRepositoryURL != null) {
-                return localRepositoryURL.getFile().getAbsoluteFile();
-            }
-        }
-        // If local repository not found assume default.
-        String localRepo = System.getProperty("user.home") + File.separator + 
".m2" + File.separator + "repository";
-        return new File(localRepo).getAbsoluteFile();
-    }
-
-    protected MavenConfiguration retrieveMavenConfiguration() {
-        MavenConfiguration mavenConfiguration = null;
-        try {
-            Configuration configuration = 
configurationAdmin.getConfiguration(ServiceConstants.PID, null);
-            if (configuration != null) {
-                Dictionary dictonary = configuration.getProperties();
-                if (dictonary != null) {
-                    DictionaryPropertyResolver resolver = new 
DictionaryPropertyResolver(dictonary);
-                    mavenConfiguration = new MavenConfigurationImpl(resolver, 
ServiceConstants.PID);
-                }
-            }
-        } catch (IOException e) {
-            logger.error("Error retrieving maven configuration",e);
-        }
-        return mavenConfiguration;
-    }
-
     /**
      * Returns the bundles that match
      * @param url
@@ -282,6 +247,59 @@ public class BundleWatcher implements Runnable, 
BundleListener {
         }
     }
 
+    public File getLocalRepository() {
+        String path = null;
+        try {
+            Configuration configuration = 
configurationAdmin.getConfiguration("org.ops4j.pax.url.mvn", null);
+            if (configuration != null) {
+                Dictionary<String, Object> dict = 
configuration.getProperties();
+                path = getLocalRepoFromConfig(dict);
+
+            }
+        } catch (Exception e) {
+            logger.error("Error retrieving maven configuration", e);
+        }
+        if (path == null) {
+            path = System.getProperty("user.home") + File.separator + ".m2" + 
File.separator + "repository";
+        }
+        int index = path.indexOf('@');
+        if (index > 0) {
+            return new File(path.substring(index)).getAbsoluteFile();
+        } else {
+            return new File(path).getAbsoluteFile();
+        }
+    }
+
+    static String getLocalRepoFromConfig(Dictionary<String, Object> dict) 
throws XMLStreamException, FileNotFoundException {
+        String path = null;
+        if (dict != null) {
+            path = (String) dict.get("org.ops4j.pax.url.mvn.localRepository");
+            if (path == null) {
+                String settings = (String) 
dict.get("org.ops4j.pax.url.mvn.settings");
+                if (settings != null) {
+                    File file = new File(settings);
+                    XMLStreamReader reader = 
XMLInputFactory.newFactory().createXMLStreamReader(new FileInputStream(file));
+                    try {
+                        int event;
+                        String elementName = null;
+                        while ((event = reader.next()) != 
XMLStreamConstants.END_DOCUMENT) {
+                            if (event == XMLStreamConstants.START_ELEMENT) {
+                                elementName = reader.getLocalName();
+                            } else if (event == 
XMLStreamConstants.END_ELEMENT) {
+                                elementName = null;
+                            } else if (event == XMLStreamConstants.CHARACTERS 
&& "localRepository".equals(elementName))  {
+                                path = reader.getText().trim();
+                            }
+                        }
+                    } finally {
+                        reader.close();
+                    }
+                }
+            }
+        }
+        return path;
+    }
+
     /**
      * Stops the execution of the thread and releases the singleton instance
      */

http://git-wip-us.apache.org/repos/asf/karaf/blob/4b310a8e/util/src/main/java/org/apache/karaf/util/maven/Parser.java
----------------------------------------------------------------------
diff --git a/util/src/main/java/org/apache/karaf/util/maven/Parser.java 
b/util/src/main/java/org/apache/karaf/util/maven/Parser.java
new file mode 100644
index 0000000..232942e
--- /dev/null
+++ b/util/src/main/java/org/apache/karaf/util/maven/Parser.java
@@ -0,0 +1,408 @@
+/*
+ * Copyright 2007 Alin Dreghiciu.
+ * Copyright 2010,2011 Toni Menzel.
+ * 
+ * Licensed  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.
+ */
+package org.apache.karaf.util.maven;
+
+import java.net.MalformedURLException;
+
+/**
+ * Parser for mvn: protocol.<br/>
+ *
+ * @author Alin Dreghiciu
+ * @author Toni Menzel
+ *
+ * @since August 10, 2007
+ */
+public class Parser
+{
+
+    /**
+     * Default version if none present in the url.
+     */
+    public static final String VERSION_LATEST = "LATEST";
+
+    /**
+     * Syntax for the url; to be shown on exception messages.
+     */
+    private static final String SYNTAX = 
"mvn:[repository_url!]groupId/artifactId[/[version]/[type]]";
+
+    /**
+     * Separator between repository and artifact definition.
+     */
+    private static final String REPOSITORY_SEPARATOR = "!";
+    /**
+     * Artifact definition segments separator.
+     */
+    private static final String ARTIFACT_SEPARATOR = "/";
+
+    /**
+     * Snapshot version
+     */
+    private static final String VERSION_SNAPSHOT = "SNAPSHOT";
+    /**
+     * Default type if not present in the url.
+     */
+    private static final String TYPE_JAR = "jar";
+
+    /**
+     * Final artifact path separator.
+     */
+    public static final String FILE_SEPARATOR = "/";
+    /**
+     * Group id path separator.
+     */
+    private static final String GROUP_SEPARATOR = "\\.";
+    /**
+     * Separator used to constructs the artifact file name.
+     */
+    private static final String VERSION_SEPARATOR = "-";
+    /**
+     * Artifact extension(type) separator.
+     */
+    private static final String TYPE_SEPARATOR = ".";
+    /**
+     * Separator used to separate classifier in artifact name.
+     */
+    private static final String CLASSIFIER_SEPARATOR = "-";
+    /**
+     * Maven metadata file.
+     */
+    private static final String METADATA_FILE = "maven-metadata.xml";
+    /**
+     * Maven local metadata file.
+     */
+    private static final String METADATA_FILE_LOCAL = 
"maven-metadata-local.xml";
+
+    /**
+     * Repository URL. Null if not present.
+     */
+    private String m_repositoryURL;
+    /**
+     * Artifact group id.
+     */
+    private String m_group;
+    /**
+     * Artifact id.
+     */
+    private String m_artifact;
+    /**
+     * Artifact version.
+     */
+    private String m_version;
+    /**
+     * Artifact type.
+     */
+    private String m_type;
+    /**
+     * Artifact classifier.
+     */
+    private String m_classifier;
+    /**
+     * Artifact classifier to use to build artifact name.
+     */
+    private String m_fullClassifier;
+
+    /**
+     * Creates a new protocol parser.
+     *
+     * @param path the path part of the url (without starting mvn:)
+     *
+     * @throws MalformedURLException if provided path does not comply to 
expected syntax or an malformed repository URL
+     */
+    public Parser( final String path )
+            throws MalformedURLException
+    {
+        if( path == null )
+        {
+            throw new MalformedURLException( "Path cannot be null. Syntax " + 
SYNTAX );
+        }
+        if( path.startsWith( REPOSITORY_SEPARATOR ) || path.endsWith( 
REPOSITORY_SEPARATOR ) )
+        {
+            throw new MalformedURLException(
+                    "Path cannot start or end with " + REPOSITORY_SEPARATOR + 
". Syntax " + SYNTAX
+            );
+        }
+        if( path.contains( REPOSITORY_SEPARATOR ) )
+        {
+            int pos = path.lastIndexOf( REPOSITORY_SEPARATOR );
+            parseArtifactPart( path.substring( pos + 1 ) );
+            m_repositoryURL = path.substring( 0, pos ) + "@snapshots";
+        }
+        else
+        {
+            parseArtifactPart( path );
+        }
+    }
+
+    /**
+     * Parses the artifact part of the url ( without the repository).
+     *
+     * @param part url part without protocol and repository.
+     *
+     * @throws MalformedURLException if provided path does not comply to 
syntax.
+     */
+    private void parseArtifactPart( final String part )
+            throws MalformedURLException
+    {
+        String[] segments = part.split( ARTIFACT_SEPARATOR );
+        if( segments.length < 2 )
+        {
+            throw new MalformedURLException( "Invalid path. Syntax " + SYNTAX 
);
+        }
+        // we must have a valid group
+        m_group = segments[ 0 ];
+        if( m_group.trim().length() == 0 )
+        {
+            throw new MalformedURLException( "Invalid groupId. Syntax " + 
SYNTAX );
+        }
+        // valid artifact
+        m_artifact = segments[ 1 ];
+        if( m_artifact.trim().length() == 0 )
+        {
+            throw new MalformedURLException( "Invalid artifactId. Syntax " + 
SYNTAX );
+        }
+        // version is optional but we have a default value 
+        m_version = VERSION_LATEST;
+        if( segments.length >= 3 && segments[ 2 ].trim().length() > 0 )
+        {
+            m_version = segments[ 2 ];
+        }
+        // type is optional but we have a default value
+        m_type = TYPE_JAR;
+        if( segments.length >= 4 && segments[ 3 ].trim().length() > 0 )
+        {
+            m_type = segments[ 3 ];
+        }
+        // classifier is optional (if not present or empty we will have a null 
classifier
+        m_fullClassifier = "";
+        if( segments.length >= 5 && segments[ 4 ].trim().length() > 0 )
+        {
+            m_classifier = segments[ 4 ];
+            m_fullClassifier = CLASSIFIER_SEPARATOR + m_classifier;
+        }
+    }
+
+    /**
+     * Returns the repository URL if present, null otherwise
+     *
+     * @return repository URL
+     */
+    public String getRepositoryURL()
+    {
+        return m_repositoryURL;
+    }
+
+    /**
+     * Returns the group id of the artifact.
+     *
+     * @return group Id
+     */
+    public String getGroup()
+    {
+        return m_group;
+    }
+
+    /**
+     * Returns the artifact id.
+     *
+     * @return artifact id
+     */
+    public String getArtifact()
+    {
+        return m_artifact;
+    }
+
+    /**
+     * Returns the artifact version.
+     *
+     * @return version
+     */
+    public String getVersion()
+    {
+        return m_version;
+    }
+
+    /**
+     * Returns the artifact type.
+     *
+     * @return type
+     */
+    public String getType()
+    {
+        return m_type;
+    }
+
+    /**
+     * Returns the artifact classifier.
+     *
+     * @return classifier
+     */
+    public String getClassifier()
+    {
+        return m_classifier;
+    }
+
+    /**
+     * Returns the complete path to artifact as stated by Maven 2 repository 
layout.
+     *
+     * @return artifact path
+     */
+    public String getArtifactPath()
+    {
+        return getArtifactPath( m_version );
+    }
+
+    /**
+     * Returns the complete path to artifact as stated by Maven 2 repository 
layout.
+     *
+     * @param version The version of the artifact.
+     *
+     * @return artifact path
+     */
+    public String getArtifactPath( final String version )
+    {
+        return new StringBuilder()
+                .append( m_group.replaceAll( GROUP_SEPARATOR, FILE_SEPARATOR ) 
)
+                .append( FILE_SEPARATOR )
+                .append( m_artifact )
+                .append( FILE_SEPARATOR )
+                .append( version )
+                .append( FILE_SEPARATOR )
+                .append( m_artifact )
+                .append( VERSION_SEPARATOR )
+                .append( version )
+                .append( m_fullClassifier )
+                .append( TYPE_SEPARATOR )
+                .append( m_type )
+                .toString();
+    }
+
+    /**
+     * Returns the version for an artifact for a snapshot version.
+     *
+     * @param version     The version of the snapshot.
+     * @param timestamp   The timestamp of the snapshot.
+     * @param buildnumber The buildnumber of the snapshot.
+     *
+     * @return artifact path
+     */
+    public String getSnapshotVersion( final String version, final String 
timestamp, final String buildnumber )
+    {
+        return version.replace( VERSION_SNAPSHOT, timestamp ) + 
VERSION_SEPARATOR + buildnumber;
+    }
+
+    /**
+     * Returns the complete path to artifact for a snapshot file.
+     *
+     * @param version     The version of the snapshot.
+     * @param timestamp   The timestamp of the snapshot.
+     * @param buildnumber The buildnumber of the snapshot.
+     *
+     * @return artifact path
+     */
+    public String getSnapshotPath( final String version, final String 
timestamp, final String buildnumber )
+    {
+        return new StringBuilder()
+                .append( m_group.replaceAll( GROUP_SEPARATOR, FILE_SEPARATOR ) 
)
+                .append( FILE_SEPARATOR )
+                .append( m_artifact )
+                .append( FILE_SEPARATOR )
+                .append( version )
+                .append( FILE_SEPARATOR )
+                .append( m_artifact )
+                .append( VERSION_SEPARATOR )
+                .append( getSnapshotVersion( version, timestamp, buildnumber ) 
)
+                .append( m_fullClassifier )
+                .append( TYPE_SEPARATOR )
+                .append( m_type )
+                .toString();
+    }
+
+    /**
+     * Returns the path to metdata file corresponding to this artifact version.
+     *
+     * @param version The version of the the metadata.
+     *
+     * @return metadata file path
+     */
+    public String getVersionMetadataPath( final String version )
+    {
+        return new StringBuilder()
+                .append( m_group.replaceAll( GROUP_SEPARATOR, FILE_SEPARATOR ) 
)
+                .append( FILE_SEPARATOR )
+                .append( m_artifact )
+                .append( FILE_SEPARATOR )
+                .append( version )
+                .append( FILE_SEPARATOR )
+                .append( METADATA_FILE )
+                .toString();
+    }
+
+    /**
+     * Returns the path to local metdata file corresponding to this artifact 
version.
+     *
+     * @param version The version of the the metadata.
+     *
+     * @return metadata file path
+     */
+    public String getVersionLocalMetadataPath( final String version )
+    {
+        return new StringBuilder()
+                .append( m_group.replaceAll( GROUP_SEPARATOR, FILE_SEPARATOR ) 
)
+                .append( FILE_SEPARATOR )
+                .append( m_artifact )
+                .append( FILE_SEPARATOR )
+                .append( version )
+                .append( FILE_SEPARATOR )
+                .append( METADATA_FILE_LOCAL )
+                .toString();
+    }
+
+    /**
+     * Returns the complete path to artifact local metadata file.
+     *
+     * @return artifact path
+     */
+    public String getArtifactLocalMetdataPath()
+    {
+        return new StringBuilder()
+                .append( m_group.replaceAll( GROUP_SEPARATOR, FILE_SEPARATOR ) 
)
+                .append( FILE_SEPARATOR )
+                .append( m_artifact )
+                .append( FILE_SEPARATOR )
+                .append( METADATA_FILE_LOCAL )
+                .toString();
+    }
+
+    /**
+     * Returns the complete path to artifact metadata file.
+     *
+     * @return artifact path
+     */
+    public String getArtifactMetdataPath()
+    {
+        return new StringBuilder()
+                .append( m_group.replaceAll( GROUP_SEPARATOR, FILE_SEPARATOR ) 
)
+                .append( FILE_SEPARATOR )
+                .append( m_artifact )
+                .append( FILE_SEPARATOR )
+                .append( METADATA_FILE )
+                .toString();
+    }
+
+}

Reply via email to