View Javadoc

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  
15  import java.util.Collection;
16  import java.util.Iterator;
17  import java.util.Properties;
18  import java.io.*;
19  
20  public class PropertyUtils
21  {
22      private static final Logger log = Logger.getLogger(PropertyUtils.class);
23  
24      public static Properties getProperties(String resource, Class callingClass)
25      {
26          return getPropertiesFromStream(ClassLoaderUtils.getResourceAsStream(resource, callingClass));
27      }
28  
29      public static Properties getPropertiesFromFile(File file)
30      {
31          try
32          {
33              return getPropertiesFromStream(new FileInputStream(file));
34          }
35          catch (FileNotFoundException e)
36          {
37              log.error("Error loading properties from file: " + file.getPath() + ". File does not exist.", e);
38              return null;
39          }
40      }
41  
42      public static Properties getPropertiesFromStream(InputStream is)
43      {
44          if (is == null)
45              return null;
46  
47          Properties props = new Properties();
48          try
49          {
50              props.load(is);
51          }
52          catch (IOException e)
53          {
54              log.error("Error loading properties from stream.", e);
55          }
56          finally
57          {
58              FileUtils.shutdownStream(is);
59          }
60  
61          return props;
62      }
63  
64  
65      /**
66       * Check to see if the two propertySet contain the same values and types
67       * NOTE If both PropertySets are null then <i>true</i> is returned
68       * @param pThis First PropertySet
69       * @param pThat Second PropertySet
70       * @return Are the two PropertySets identical
71       */
72      public static boolean identical(PropertySet pThis, PropertySet pThat)
73      {
74          //Check to see if both of the collections are null
75          if (pThis == null && pThat == null)
76              return true;
77  
78          //Check to see if either of the collections are null
79          if (pThis == null || pThat == null)
80              return false;
81  
82          Collection thisKeys = pThis.getKeys();
83          Collection thatKeys = pThat.getKeys();
84  
85          if (!thisKeys.containsAll(thatKeys) || !thatKeys.containsAll(thisKeys))
86              return false;
87  
88          Iterator thisKeysIterator = thisKeys.iterator();
89          String key;
90          int keyType;
91          while (thisKeysIterator.hasNext())
92          {
93              key = (String) thisKeysIterator.next();
94              keyType = pThis.getType(key);
95              if (PropertySet.BOOLEAN == keyType)
96              {
97                  if (pThis.getBoolean(key) != pThat.getBoolean(key))
98                      return false;
99              }
100             else if (PropertySet.DATA == keyType)
101             {
102                 throw new IllegalArgumentException("DATA Comparision has not been implemented in PropertyUtil");
103             }
104             else if (PropertySet.DATE == keyType)
105             {
106                 if (!pThis.getDate(key).equals(pThat.getDate(key)))
107                     return false;
108             }
109             else if (PropertySet.DOUBLE == keyType)
110             {
111                 if (pThis.getDouble(key) != pThat.getDouble(key))
112                     return false;
113             }
114             else if (PropertySet.INT == keyType)
115             {
116                 if (pThis.getInt(key) != pThat.getInt(key))
117                     return false;
118             }
119             else if (PropertySet.OBJECT == keyType)
120             {
121                 throw new IllegalArgumentException("OBJECT Comparision has not been implemented in PropertyUtil");
122             }
123             else if (PropertySet.PROPERTIES == keyType)
124             {
125                 throw new IllegalArgumentException("PROPERTIES Comparision has not been implemented in PropertyUtil");
126             }
127             else if (PropertySet.LONG == keyType)
128             {
129                 if (pThis.getLong(key) != pThat.getLong(key))
130                     return false;
131             }
132             else if (PropertySet.STRING == keyType)
133             {
134                 if (!pThis.getString(key).equals(pThat.getString(key)))
135                     return false;
136             }
137             else if (PropertySet.TEXT == keyType)
138             {
139                 if (!pThis.getText(key).equals(pThat.getText(key)))
140                     return false;
141             }
142             else if (PropertySet.XML == keyType)
143             {
144                 throw new IllegalArgumentException("XML Comparision has not been implemented in PropertyUtil");
145             }
146         }
147 
148         return true;
149     }
150 
151 }