1 /*
2  * Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation. Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 package org.netbeans.jemmy;
26 
27 import java.io.FileNotFoundException;
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.io.PrintStream;
31 import java.io.PrintWriter;
32 import java.util.Enumeration;
33 import java.util.Hashtable;
34 import java.util.zip.ZipException;
35 
36 /**
37  *
38  * Provides functionality to work with a bunch of resource files. <BR>
39  *
40  * @see org.netbeans.jemmy.Bundle
41  *
42  * @author Alexandre Iline (alexandre.iline@oracle.com)
43  */
44 public class BundleManager {
45 
46     private Hashtable<String, Bundle> bundles;
47 
48     /**
49      * Bundle manager constructor.
50      */
BundleManager()51     public BundleManager() {
52         bundles = new Hashtable<>();
53         try {
54             load();
55         } catch (IOException ignored) {
56         }
57     }
58 
59     /**
60      * Adds a Bundle to the managed collection of resource files.
61      *
62      * @param bundle Bundle object
63      * @param ID Symbolic bundle id
64      * @return First parameter or null if bundle with ID already exists.
65      * @see org.netbeans.jemmy.Bundle
66      */
addBundle(Bundle bundle, String ID)67     public Bundle addBundle(Bundle bundle, String ID) {
68         if (getBundle(ID) != null) {
69             return null;
70         } else {
71             bundles.put(ID, bundle);
72             return bundle;
73         }
74     }
75 
76     /**
77      * Removes a Bundle from the managed collection of resource files.
78      *
79      * @param ID Symbolic bundle id
80      * @return Removed bundle or null if no bundle ID is.
81      */
removeBundle(String ID)82     public Bundle removeBundle(String ID) {
83         Bundle value = getBundle(ID);
84         bundles.remove(ID);
85         return value;
86     }
87 
88     /**
89      * Returns a Bundle given it's symbolic ID.
90      *
91      * @param ID Symbolic bundle ID
92      * @return the Bundle. A null reference is returned if no bundle with the
93      * symbolic ID was found.
94      */
getBundle(String ID)95     public Bundle getBundle(String ID) {
96         return bundles.get(ID);
97     }
98 
99     /**
100      * Create a new Bundle, load resources from a simple text file, and add the
101      * bundle. Load resources from a text file to a new Bundle object. The new
102      * Bundle is added to the collection of objects managed by this
103      * {@code BundleManager}.
104      *
105      * @param fileName Name of a file to load resources from.
106      * @param ID Symbolic bundle ID used to identify the new bundle used to
107      * manage the resources from the file.
108      * @return a newly created bundle.
109      * @exception IOException
110      * @exception FileNotFoundException
111      */
loadBundleFromFile(String fileName, String ID)112     public Bundle loadBundleFromFile(String fileName, String ID)
113             throws IOException, FileNotFoundException {
114         if (getBundle(ID) != null) {
115             return null;
116         }
117         Bundle bundle = new Bundle();
118         bundle.loadFromFile(fileName);
119         return addBundle(bundle, ID);
120     }
121 
loadBundleFromStream(InputStream stream, String ID)122     public Bundle loadBundleFromStream(InputStream stream, String ID)
123             throws IOException, FileNotFoundException {
124         if (getBundle(ID) != null) {
125             return null;
126         }
127         Bundle bundle = new Bundle();
128         bundle.load(stream);
129         return addBundle(bundle, ID);
130     }
131 
loadBundleFromResource(ClassLoader cl, String resource, String ID)132     public Bundle loadBundleFromResource(ClassLoader cl, String resource, String ID)
133             throws IOException, FileNotFoundException {
134         return loadBundleFromStream(cl.getResourceAsStream(resource), ID);
135     }
136 
137     /**
138      * Loads resources from simple text file pointed by jemmy.resources system
139      * property. The resources are loaded into the Bundle with ID "". Does not
140      * do anything if jemmy.resources has not been set or is empty.
141      *
142      * @return a newly created bundle.
143      * @exception IOException
144      * @exception FileNotFoundException
145      */
load()146     public Bundle load()
147             throws IOException, FileNotFoundException {
148         if (System.getProperty("jemmy.resources") != null
149                 && !System.getProperty("jemmy.resources").equals("")) {
150             return loadBundleFromFile(System.getProperty("jemmy.resources"), "");
151         }
152         return null;
153     }
154 
155     /**
156      * Loads resources from file in jar archive into new Bundle object and adds
157      * it.
158      *
159      * @param fileName Name of jar file.
160      * @param entryName ?enryName? Name of file to load resources from.
161      * @param ID Symbolic bundle id
162      * @return a newly created bundle.
163      * @exception IOException
164      * @exception FileNotFoundException
165      */
loadBundleFromJar(String fileName, String entryName, String ID)166     public Bundle loadBundleFromJar(String fileName, String entryName, String ID)
167             throws IOException, FileNotFoundException {
168         if (getBundle(ID) != null) {
169             return null;
170         }
171         Bundle bundle = new Bundle();
172         bundle.loadFromJar(fileName, entryName);
173         return addBundle(bundle, ID);
174     }
175 
176     /**
177      * Loads resources from file in zip archive into new Bundle object and adds
178      * it.
179      *
180      * @param fileName Name of jar file.
181      * @param entryName ?enryName? Name of file to load resources from.
182      * @param ID Symbolic bundle id
183      * @return a newly created bundle.
184      * @exception ZipException
185      * @exception IOException
186      * @exception FileNotFoundException
187      */
loadBundleFromZip(String fileName, String entryName, String ID)188     public Bundle loadBundleFromZip(String fileName, String entryName, String ID)
189             throws IOException, FileNotFoundException, ZipException {
190         if (getBundle(ID) != null) {
191             return null;
192         }
193         Bundle bundle = new Bundle();
194         bundle.loadFromZip(fileName, entryName);
195         return addBundle(bundle, ID);
196     }
197 
198     /**
199      * Prints bundles contents.
200      *
201      * @param writer Writer to print data in.
202      */
print(PrintWriter writer)203     public void print(PrintWriter writer) {
204         Enumeration<String> keys = bundles.keys();
205         Bundle bundle;
206         String key;
207         while (keys.hasMoreElements()) {
208             key = keys.nextElement();
209             writer.println("\"" + key + "\" bundle contents");
210             bundle = getBundle(key);
211             bundle.print(writer);
212         }
213     }
214 
215     /**
216      * Prints bundles contents.
217      *
218      * @param stream Stream to print data in.
219      */
print(PrintStream stream)220     public void print(PrintStream stream) {
221         print(new PrintWriter(stream));
222     }
223 
224     /**
225      * Returns resource from ID bundle.
226      *
227      * @param bundleID Bundle ID.
228      * @param key Resource key.
229      * @return the resource value. If the bundle ID does not exist if the
230      * resource with the given key cannot be found, a null reference is
231      * returned.
232      */
getResource(String bundleID, String key)233     public String getResource(String bundleID, String key) {
234         Bundle bundle = getBundle(bundleID);
235         if (bundle != null) {
236             return bundle.getResource(key);
237         }
238         return null;
239     }
240 
241     /**
242      * Searches for a resource in all the managed Bundles.
243      *
244      * @param key Resource key.
245      * @return first resource value found that is indexed by the given key. If
246      * no resource is found, return a null reference.
247      */
getResource(String key)248     public String getResource(String key) {
249         Enumeration<Bundle> data = bundles.elements();
250         String value;
251         while (data.hasMoreElements()) {
252             value = data.nextElement().getResource(key);
253             if (value != null) {
254                 return value;
255             }
256         }
257         return null;
258     }
259 
260     /**
261      * Counts the number of resource occurences in all the managed Bundles.
262      *
263      * @param key Resource key
264      * @return the number of resource occurences with the given key among all
265      * the Bundles managed by this BundleManager.
266      */
calculateResources(String key)267     public int calculateResources(String key) {
268         Enumeration<Bundle> data = bundles.elements();
269         int count = 0;
270         while (data.hasMoreElements()) {
271             if (data.nextElement().getResource(key) != null) {
272                 count++;
273             }
274         }
275         return count;
276     }
277 
278     /**
279      * Creates a shallow copy of this BundleManager. Does not copy bundles, only
280      * their references.
281      *
282      * @return a copy of this BundleManager.
283      */
cloneThis()284     public BundleManager cloneThis() {
285         BundleManager result = new BundleManager();
286         Enumeration<String> keys = bundles.keys();
287         Enumeration<Bundle> elements = bundles.elements();
288         while (keys.hasMoreElements()) {
289             result.bundles.put(keys.nextElement(),
290                     elements.nextElement());
291         }
292         return result;
293     }
294 }
295