1   /*
2    * Created by IntelliJ IDEA.
3    * User: Administrator
4    * Date: 28/02/2002
5    * Time: 11:48:12
6    * To change template for new class use
7    * Code Style | Class Templates options (Tools | IDE Options).
8    */
9   package com.atlassian.core.util;
10  
11  import com.opensymphony.module.propertyset.PropertySet;
12  import org.apache.log4j.Category;
13  import org.apache.log4j.Logger;
14  import org.apache.commons.io.IOUtils;
15  
16  import java.util.Collection;
17  import java.util.Iterator;
18  import java.util.Properties;
19  import java.io.*;
20  import java.net.URL;
21  
22  public class PropertyUtils
23  {
24      private static final Logger log = Logger.getLogger(PropertyUtils.class);
25  
26      public static Properties getProperties(String resource, Class callingClass)
27      {
28          final URL url = callingClass.getResource(resource);
29          InputStream is = null;
30          try
31          {
32              is = url.openStream();
33              return getPropertiesFromStream(is);
34          }
35          catch (IOException e)
36          {
37              return null;
38          }
39          finally
40          {
41              IOUtils.closeQuietly(is);
42          }
43      }
44  
45      public static Properties getPropertiesFromFile(File file)
46      {
47          try
48          {
49              return getPropertiesFromStream(new FileInputStream(file));
50          }
51          catch (FileNotFoundException e)
52          {
53              log.error("Error loading properties from file: " + file.getPath() + ". File does not exist.", e);
54              return null;
55          }
56      }
57  
58      public static Properties getPropertiesFromStream(InputStream is)
59      {
60          if (is == null)
61              return null;
62  
63          Properties props = new Properties();
64          try
65          {
66              props.load(is);
67          }
68          catch (IOException e)
69          {
70              log.error("Error loading properties from stream.", e);
71          }
72          finally
73          {
74              IOUtils.closeQuietly(is);
75          }
76  
77          return props;
78      }
79  
80  
81      /**
82       * Check to see if the two propertySet contain the same values and types
83       * NOTE If both PropertySets are null then <i>true</i> is returned
84       * @param pThis First PropertySet
85       * @param pThat Second PropertySet
86       * @return Are the two PropertySets identical
87       */
88      public static boolean identical(PropertySet pThis, PropertySet pThat)
89      {
90          //Check to see if both of the collections are null
91          if (pThis == null && pThat == null)
92              return true;
93  
94          //Check to see if either of the collections are null
95          if (pThis == null || pThat == null)
96              return false;
97  
98          Collection thisKeys = pThis.getKeys();
99          Collection thatKeys = pThat.getKeys();
100 
101         if (!thisKeys.containsAll(thatKeys) || !thatKeys.containsAll(thisKeys))
102             return false;
103 
104         Iterator thisKeysIterator = thisKeys.iterator();
105         String key;
106         int keyType;
107         while (thisKeysIterator.hasNext())
108         {
109             key = (String) thisKeysIterator.next();
110             keyType = pThis.getType(key);
111             if (PropertySet.BOOLEAN == keyType)
112             {
113                 if (pThis.getBoolean(key) != pThat.getBoolean(key))
114                     return false;
115             }
116             else if (PropertySet.DATA == keyType)
117             {
118                 throw new IllegalArgumentException("DATA Comparision has not been implemented in PropertyUtil");
119             }
120             else if (PropertySet.DATE == keyType)
121             {
122                 if (!pThis.getDate(key).equals(pThat.getDate(key)))
123                     return false;
124             }
125             else if (PropertySet.DOUBLE == keyType)
126             {
127                 if (pThis.getDouble(key) != pThat.getDouble(key))
128                     return false;
129             }
130             else if (PropertySet.INT == keyType)
131             {
132                 if (pThis.getInt(key) != pThat.getInt(key))
133                     return false;
134             }
135             else if (PropertySet.OBJECT == keyType)
136             {
137                 throw new IllegalArgumentException("OBJECT Comparision has not been implemented in PropertyUtil");
138             }
139             else if (PropertySet.PROPERTIES == keyType)
140             {
141                 throw new IllegalArgumentException("PROPERTIES Comparision has not been implemented in PropertyUtil");
142             }
143             else if (PropertySet.LONG == keyType)
144             {
145                 if (pThis.getLong(key) != pThat.getLong(key))
146                     return false;
147             }
148             else if (PropertySet.STRING == keyType)
149             {
150                 if (!pThis.getString(key).equals(pThat.getString(key)))
151                     return false;
152             }
153             else if (PropertySet.TEXT == keyType)
154             {
155                 if (!pThis.getText(key).equals(pThat.getText(key)))
156                     return false;
157             }
158             else if (PropertySet.XML == keyType)
159             {
160                 throw new IllegalArgumentException("XML Comparision has not been implemented in PropertyUtil");
161             }
162         }
163 
164         return true;
165     }
166 
167 }