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