2011/08/05 - Jakarta Cactus has been retired.

For more information, please explore the Attic.

View Javadoc

1   /* 
2    * ========================================================================
3    * 
4    * Licensed to the Apache Software Foundation (ASF) under one or more
5    * contributor license agreements.  See the NOTICE file distributed with
6    * this work for additional information regarding copyright ownership.
7    * The ASF licenses this file to You under the Apache License, Version 2.0
8    * (the "License"); you may not use this file except in compliance with
9    * the License.  You may obtain a copy of the License at
10   * 
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   * 
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   * 
19   * ========================================================================
20   */
21  package org.apache.cactus.eclipse.runner.common;
22  
23  import java.io.File;
24  import java.io.IOException;
25  
26  import org.apache.cactus.eclipse.runner.ui.CactusPlugin;
27  import org.eclipse.core.runtime.IPath;
28  import org.eclipse.core.runtime.Path;
29  import org.eclipse.core.runtime.Platform;
30  import org.eclipse.jdt.core.IClasspathEntry;
31  import org.eclipse.jdt.core.JavaCore;
32  
33  /**
34   * Helper class for library access.
35   * 
36   * @version $Id: LibraryHelper.java 238816 2004-02-29 16:36:46Z vmassol $
37   */
38  public class LibraryHelper
39  {
40      /**
41       * Returns an array of jar paths contained
42       * in the given directory.
43       * @param theDirectory the directory to list jars from
44       * @return an array of jar paths
45       */
46      private static IPath[] getJarPathArray(File theDirectory)
47      {
48          File[] files = theDirectory.listFiles(new JarFilenameFilter());
49          IPath[] result = new IPath[files.length];
50          for (int i = 0; i < files.length; i++)
51          {
52              result[i] = new Path(files[i].getAbsolutePath());
53          }
54          return result;
55      }
56  
57      /**
58       * @return an IPath array of the jar files in client lib directory
59       */
60      private static IPath[] getClientJarPathArray()
61      {
62          File clientLibDir = getClientLibPath().toFile();
63          return getJarPathArray(clientLibDir);
64      }
65  
66      /**
67       * @return an IPath array of the jar files in client lib directory
68       */
69      private static IPath[] getCommonJarPathArray()
70      {
71          File commmonLibDir = getCommonLibPath().toFile();
72          return getJarPathArray(commmonLibDir);
73      }
74  
75      /**
76       * @return an array of IClasspathEntry contained in the client dir
77       */
78      public static IClasspathEntry[] getClientEntries()
79      {
80          IPath[] clientJars = getClientJarPathArray();
81          IClasspathEntry[] result = new IClasspathEntry[clientJars.length];
82          for (int i = 0; i < clientJars.length; i++)
83          {
84              result[i] = getIClasspathEntry(clientJars[i]);
85          }
86          return result;
87      }
88  
89      /**
90       * @return an array of IClasspathEntry contained in the common dir
91       */
92      public static IClasspathEntry[] getCommonEntries()
93      {
94          IPath[] commonJars = getCommonJarPathArray();
95          IClasspathEntry[] result = new IClasspathEntry[commonJars.length];
96          for (int i = 0; i < commonJars.length; i++)
97          {
98              result[i] =  getIClasspathEntry(commonJars[i]);
99          }
100         return result;
101     }
102 
103     /**
104      * @return an array of IClasspathEntry contained in the common dir
105      */
106     public static IClasspathEntry[] getClientSideEntries()
107     {
108         IClasspathEntry[] clientEntries = getClientEntries();
109         IClasspathEntry[] commonEntries = getCommonEntries();
110         return concatenateEntries(clientEntries, commonEntries);
111     }
112 
113     /**
114      * Concatenate two IClasspathEntry arrays.
115      * 
116      * @param theArray1 the first array
117      * @param theArray2 the second array
118      * @return an IClasspathEntry array containing the first array
119      *         followed by the second one
120      */
121     public static IClasspathEntry[] concatenateEntries(
122         IClasspathEntry[] theArray1, IClasspathEntry[] theArray2)
123     {
124         IClasspathEntry[] newArray =
125             new IClasspathEntry[theArray1.length + theArray2.length];
126         System.arraycopy(theArray1, 0, newArray, 0, theArray1.length);
127         System.arraycopy(
128             theArray2,
129             0,
130             newArray,
131             theArray1.length,
132             theArray2.length);
133         return newArray;
134     }
135 
136     /**
137      * @return the path to the library directory
138      */
139     public static IPath getLibPath()
140     {
141         CactusPlugin thePlugin = CactusPlugin.getDefault();
142         
143         try {
144 				return new Path(Platform.asLocalURL(thePlugin.getBundle().getEntry("/" +CactusPlugin.CACTUS_LIBRARY_PATH)).getPath());
145         }  catch(IOException ex) {
146 			   CactusPlugin.log(ex);	// throwing an exception would be preferable
147         }
148         return new Path(CactusPlugin.CACTUS_LIBRARY_PATH);
149     }
150 
151     /**
152      * @return the path to the client library directory
153      */
154     private static IPath getClientLibPath()
155     {
156         return getLibPath().append(CactusPlugin.CACTUS_CLIENT_LIBRARY_PATH);
157     }
158 
159     /**
160      * @return the path to the common library directory
161      */
162     private static IPath getCommonLibPath()
163     {
164         return getLibPath().append(CactusPlugin.CACTUS_COMMON_LIBRARY_PATH);
165     }
166 
167     /**
168      * @param thePath path to convert to an IClasspathEntry
169      * @return the IClasspathEntry built from the given path
170      */
171     public static IClasspathEntry getIClasspathEntry(IPath thePath)
172     {
173         return JavaCore.newLibraryEntry(thePath, null, null);
174     }
175     
176     /**
177      * @param thePath path to convert to an IClasspathEntry
178      * @return the IClasspathEntry built from the given path
179      */
180     public static IClasspathEntry getIClasspathEntry(String thePath)
181     {
182         return getIClasspathEntry(new Path(thePath));
183     }
184 }