View Javadoc

1   /*
2    * Copyright 2005-2008 The Kuali Foundation
3    * 
4    * 
5    * Licensed under the Educational Community License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    * 
9    * http://www.opensource.org/licenses/ecl2.php
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.kuali.rice.kew.engine.node;
18  
19  import java.io.IOException;
20  import java.io.InputStream;
21  import java.io.InputStreamReader;
22  import java.util.Iterator;
23  
24  import org.apache.log4j.Logger;
25  import org.kuali.rice.kew.engine.RouteContext;
26  import org.kuali.rice.kew.engine.node.var.Property;
27  import org.kuali.rice.kew.engine.node.var.PropertyScheme;
28  
29  
30  /**
31   * A utility class for reading properties from a document.
32   *
33   * @author Kuali Rice Team (rice.collab@kuali.org)
34   */
35  public final class PropertiesUtil {
36      private static final Logger LOG = Logger.getLogger(PropertiesUtil.class);
37  
38  	private PropertiesUtil() {
39  		throw new UnsupportedOperationException("do not call");
40  	}
41  
42      public static String readResource(InputStream stream) throws IOException {
43          StringBuffer sb = new StringBuffer(2048);
44          InputStreamReader reader = new InputStreamReader(stream);
45          char[] buf = new char[1024];
46          int read;
47          try {
48              while ((read = reader.read(buf)) != -1) {
49                  sb.append(buf, 0, read);
50              }
51          } finally {
52              reader.close();
53          }
54          return sb.toString();
55      }
56  
57      /**
58       * Resolves the specified name as a qualified property
59       * @param name the qualified property name
60       * @return value if found, null otherwise
61       */
62      public static Object retrieveProperty(String name, RouteContext context) {
63          return retrieveProperty(new Property(name), context);
64      }
65  
66      /**
67       * Resolves the specified name as an unqualified property
68       * @param name the potentially unqualified property name
69       * @param defaultScheme the default scheme to use if the property is unqualified
70       * @return value if found, null otherwise
71       */
72      public static Object retrieveProperty(String name, PropertyScheme defaultScheme, RouteContext context) {
73          return retrieveProperty(new Property(name), defaultScheme, context);
74      }
75  
76      /**
77       * Resolves the specified name as an unqualified property
78       * @param prop the potentially unqualified property
79       * @param defaultScheme the default scheme to use if the property is unqualified
80       * @return value if found, null otherwise
81       */
82      public static Object retrieveProperty(Property prop, PropertyScheme defaultScheme, RouteContext context) {
83          if (prop.scheme == null && defaultScheme != null) {
84              prop.scheme = defaultScheme.getName();
85          }
86          return retrieveProperty(prop, context);
87      }
88  
89      /**
90       * Resolves the specified name as a qualified property
91       * @param prop the qualified property
92       * @return value if found, null otherwise
93       */
94      public static Object retrieveProperty(Property prop, RouteContext context) {
95          Iterator schemes = PropertyScheme.SCHEMES.iterator();
96          while (schemes.hasNext()) {
97              PropertyScheme scheme = (PropertyScheme) schemes.next();
98              if (scheme.getName().equals(prop.scheme) ||
99                  scheme.getShortName().equals(prop.scheme)) {
100                 LOG.debug("Loading prop " + prop + " with scheme " + scheme);
101                 return scheme.load(prop, context);
102             }
103         }
104         String message = "Invalid property scheme: '" + prop.scheme + "'"; 
105         LOG.error(message);
106         throw new RuntimeException(message);
107     }
108 }