Rev 1306 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1 | fvelez | 1 | package jomm.utils; |
2 | |||
3 | /** |
||
4 | * |
||
5 | * @author Jorge Machado |
||
6 | * @version |
||
7 | */ |
||
8 | |||
9 | import java.io.*; |
||
10 | import java.net.URL; |
||
11 | import java.util.*; |
||
12 | |||
13 | import org.apache.log4j.Logger; |
||
14 | |||
15 | public class AppProperties extends 1.5.0/docs/api/java/util/Properties.html">Properties |
||
16 | { |
||
17 | private static final 1.5.0/docs/api/java/util/logging/Logger.html">Logger logger = 1.5.0/docs/api/java/util/logging/Logger.html">Logger.getLogger(AppProperties.class); |
||
18 | |||
19 | |||
20 | public static 1.5.0/docs/api/java/lang/String.html">String getPathOfResource(1.5.0/docs/api/java/lang/String.html">String resourceName){ |
||
21 | |||
22 | 1.5.0/docs/api/java/net/URL.html">URL url = 1.5.0/docs/api/java/lang/Thread.html">Thread.currentThread().getContextClassLoader().getResource(resourceName); |
||
23 | if (url==null) |
||
24 | return null; |
||
25 | 1.5.0/docs/api/java/lang/String.html">String ret=null; |
||
26 | try{ |
||
27 | ret=java.net.1.5.0/docs/api/java/net/URLDecoder.html">URLDecoder.decode(url.getFile(),"iso-8859-1"); |
||
28 | }catch(1.5.0/docs/api/java/io/UnsupportedEncodingException.html">UnsupportedEncodingException e){ |
||
29 | throw new 1.5.0/docs/api/java/lang/RuntimeException.html">RuntimeException(e); |
||
30 | } |
||
31 | if (ret==null) |
||
32 | return null; |
||
33 | if (ret.startsWith("/")) |
||
34 | return ret.substring(1); |
||
35 | return ret; |
||
36 | } |
||
37 | |||
38 | public AppProperties(1.5.0/docs/api/java/lang/String.html">String filename) throws 1.5.0/docs/api/java/io/IOException.html">IOException { |
||
39 | 5+0%2Fdocs%2Fapi+InputStream">InputStream file = getClass().getClassLoader().getResourceAsStream(filename); |
||
40 | int dot = filename.indexOf("."); |
||
41 | 1.5.0/docs/api/java/lang/String.html">String startFilename = filename.substring(0,filename.indexOf(".")); |
||
42 | 5+0%2Fdocs%2Fapi+InputStream">InputStream fileEdited = getClass().getClassLoader().getResourceAsStream(startFilename + "Edited" + filename.substring(dot)); |
||
43 | load(file); |
||
44 | if(fileEdited!=null){ |
||
45 | 1.5.0/docs/api/java/lang/System.html">System.out.println(filename + " >> " + startFilename + "Edited" + filename.substring(dot)); |
||
46 | load(fileEdited); |
||
47 | } |
||
48 | |||
49 | } |
||
50 | |||
51 | public AppProperties() { |
||
52 | |||
53 | } |
||
54 | |||
55 | public void loadPropertiesCompleteFilePath(1.5.0/docs/api/java/lang/String.html">String filename) throws 1.5.0/docs/api/java/io/IOException.html">IOException { |
||
56 | 5+0%2Fdocs%2Fapi+InputStream">InputStream file = (5+0%2Fdocs%2Fapi+InputStream">InputStream)new 1.5.0/docs/api/java/io/FileInputStream.html">FileInputStream(filename); |
||
57 | load(file); |
||
58 | } |
||
59 | public AppProperties(1.5.0/docs/api/java/net/URL.html">URL filename) throws 1.5.0/docs/api/java/io/IOException.html">IOException { |
||
60 | load(filename.openConnection().getInputStream()); |
||
61 | } |
||
62 | |||
63 | /*********************************************************** |
||
64 | * Returns a list of all property names hat starts with the given |
||
65 | * prefix |
||
66 | * |
||
67 | ************************************************************/ |
||
68 | public 5+0%2Fdocs%2Fapi+List">List getListProperties(1.5.0/docs/api/java/lang/String.html">String propertyPrefix){ |
||
69 | |||
70 | 5+0%2Fdocs%2Fapi+List">List props = new 1.5.0/docs/api/java/util/ArrayList.html">ArrayList(); |
||
71 | 1.5.0/docs/api/java/util/Enumeration.html">Enumeration enumE; |
||
72 | try{ |
||
73 | enumE = propertyNames(); |
||
74 | } |
||
75 | catch(1.5.0/docs/api/java/lang/Exception.html">Exception e){ |
||
76 | logger.fatal("cant load properties"); |
||
77 | return null; |
||
78 | } |
||
79 | |||
80 | while (enumE.hasMoreElements()) { |
||
81 | 1.5.0/docs/api/java/lang/String.html">String propertyName = (1.5.0/docs/api/java/lang/String.html">String)enumE.nextElement(); |
||
82 | if (propertyName.startsWith(propertyPrefix)) { |
||
83 | props.add(propertyName); |
||
84 | } |
||
85 | } |
||
86 | 1.5.0/docs/api/java/util/Collections.html">Collections.sort(props,StringComparator.getInstance()); |
||
87 | return props; |
||
88 | } |
||
89 | |||
90 | public 5+0%2Fdocs%2Fapi+List">List getListValuesProperties(1.5.0/docs/api/java/lang/String.html">String propertyPrefix){ |
||
91 | |||
92 | 5+0%2Fdocs%2Fapi+List">List props = new 1.5.0/docs/api/java/util/ArrayList.html">ArrayList(); |
||
93 | 1.5.0/docs/api/java/util/Enumeration.html">Enumeration enumE; |
||
94 | try{ |
||
95 | enumE = propertyNames(); |
||
96 | } |
||
97 | catch(1.5.0/docs/api/java/lang/Exception.html">Exception e){ |
||
98 | logger.fatal("cant load properties"); |
||
99 | return null; |
||
100 | } |
||
101 | while (enumE.hasMoreElements()) { |
||
102 | 1.5.0/docs/api/java/lang/String.html">String propertyName = (1.5.0/docs/api/java/lang/String.html">String)enumE.nextElement(); |
||
103 | if (propertyName.startsWith(propertyPrefix)) { |
||
104 | props.add(getProperty(propertyName)); |
||
105 | } |
||
106 | } |
||
107 | 1.5.0/docs/api/java/util/Collections.html">Collections.sort(props,StringComparator.getInstance()); |
||
108 | return props; |
||
109 | } |
||
110 | } |