View Javadoc

1   /*
2    * Copyright 2012-2013 smartics, Kronseder & Reiner GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package de.smartics.properties.resource.repository;
17  
18  import java.io.Serializable;
19  import java.net.URL;
20  import java.util.ArrayList;
21  import java.util.Collections;
22  import java.util.HashMap;
23  import java.util.List;
24  import java.util.Map;
25  
26  import javax.annotation.CheckForNull;
27  import javax.annotation.concurrent.NotThreadSafe;
28  
29  import org.slf4j.Logger;
30  import org.slf4j.LoggerFactory;
31  
32  import de.smartics.util.lang.Arguments;
33  
34  /**
35   * Defines the environment of class path resources to access.
36   */
37  @NotThreadSafe
38  public final class ClassPathEnvironment implements Serializable
39  {
40    // ********************************* Fields *********************************
41  
42    // --- constants ------------------------------------------------------------
43  
44    /**
45     * The class version identifier.
46     */
47    private static final long serialVersionUID = 1L;
48  
49    /**
50     * Reference to the logger for this class.
51     */
52    private static final Logger LOG = LoggerFactory
53        .getLogger(ClassPathEnvironment.class);
54  
55    // --- members --------------------------------------------------------------
56  
57    /**
58     * The map of artifact IDs (GAV concatenated and separated by colons) to the
59     * artifact references.
60     */
61    private final Map<String, ArtifactRef> index =
62        new HashMap<String, ArtifactRef>();
63  
64    /**
65     * The list of artifact references found on this class path.
66     *
67     * @serial
68     */
69    private final List<ArtifactRef> artifactRefs = new ArrayList<ArtifactRef>();
70  
71    // ****************************** Initializer *******************************
72  
73    // ****************************** Constructors ******************************
74  
75    /**
76     * Default constructor.
77     */
78    public ClassPathEnvironment()
79    {
80    }
81  
82    /**
83     * Convenience constructor that allows to add artifact references to create an
84     * initial list.
85     *
86     * @param artifactRefs the initial collection of artifact references to add as
87     *          roots.
88     */
89    public ClassPathEnvironment(final ArtifactRef... artifactRefs)
90    {
91      this();
92  
93      for (final ArtifactRef artifactRef : artifactRefs)
94      {
95        addArtifactRef(artifactRef);
96      }
97    }
98  
99    /**
100    * Convenience constructor that allows to add artifact references to create an
101    * initial list.
102    *
103    * @param artifactRefs the initial collection of artifact references to add as
104    *          roots.
105    */
106   public ClassPathEnvironment(final List<ArtifactRef> artifactRefs)
107   {
108     for (final ArtifactRef artifactRef : artifactRefs)
109     {
110       addArtifactRef(artifactRef);
111     }
112   }
113 
114   // ****************************** Inner Classes *****************************
115 
116   // ********************************* Methods ********************************
117 
118   // --- init -----------------------------------------------------------------
119 
120   // --- get&set --------------------------------------------------------------
121 
122   // --- business -------------------------------------------------------------
123   /**
124    * Returns a list of artifact references.
125    *
126    * @return an unmodifiable list of artifact references registered in this
127    *         environment.
128    */
129   public List<ArtifactRef> getArtifactRefs()
130   {
131     return Collections.unmodifiableList(artifactRefs);
132   }
133 
134   /**
135    * Returns a list of URLs that point to the physical locations of the
136    * artifacts.
137    *
138    * @return a list of URLs that point to the physical locations of the
139    *         artifacts.
140    */
141   public List<URL> getUrls()
142   {
143     final List<URL> urls = new ArrayList<URL>(artifactRefs.size());
144     for (final URL url : urls)
145     {
146       urls.add(url);
147     }
148 
149     return urls;
150   }
151 
152   /**
153    * Returns the artifact reference by its short ID.
154    *
155    * @param artifactId the GAV concatenated and separated by colons.
156    * @return the associated artifact reference or <code>null</code> if there is
157    *         no artifact reference with the given {@code artifactId} registered.
158    * @throws NullPointerException if {@code artifactId} is <code>null</code>.
159    */
160   @CheckForNull
161   public ArtifactRef getArtifactRef(final String artifactId)
162     throws NullPointerException
163   {
164     Arguments.checkNotNull("artifactId", artifactId);
165 
166     return index.get(artifactId);
167   }
168 
169   /**
170    * Adds the given artifact reference to the list of artifact references. All
171    * references are considered to be roots to search for class path resources.
172    *
173    * @param artifactRef the artifact reference to add.
174    * @throws NullPointerException if {@code artifactRef} is <code>null</code>.
175    */
176   public void addArtifactRef(final ArtifactRef artifactRef)
177     throws NullPointerException
178   {
179     Arguments.checkNotNull("artifactRef", artifactRef);
180 
181     if (!artifactRef.isArchive())
182     {
183       LOG.debug("Dropping '" + artifactRef + "' since it is not an archive.");
184       return;
185     }
186 
187     artifactRefs.add(artifactRef);
188     final String artifactId = artifactRef.getId().toShortString();
189     index.put(artifactId, artifactRef);
190   }
191 
192   // --- object basics --------------------------------------------------------
193 
194   /**
195    * Returns the string representation of the object.
196    *
197    * @return the string representation of the object.
198    */
199   @Override
200   public String toString()
201   {
202     final StringBuilder buffer = new StringBuilder();
203 
204     buffer.append("Class path roots:");
205 
206     for (final ArtifactRef artifactRef : artifactRefs)
207     {
208       buffer.append(' ').append(artifactRef);
209     }
210 
211     return buffer.toString();
212   }
213 }