View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.onami.autobind.utils;
21  
22  import java.util.StringTokenizer;
23  
24  import javax.inject.Inject;
25  
26  import org.apache.onami.autobind.jsr330.Names;
27  
28  import com.google.inject.AbstractModule;
29  import com.google.inject.Guice;
30  import com.google.inject.Injector;
31  import com.google.inject.Key;
32  
33  
34  public class VariableResolver {
35  
36      /**
37       * The symbol that indicates a variable begin.
38       */
39      private static final String VAR_BEGIN = "$";
40  
41      /**
42       * The symbol that separates the key name to the default value.
43       */
44      private static final String PIPE_SEPARATOR = "|";
45      
46      private static final String KEY_PREFIX = "${";
47  
48      /**
49       * The Injector instance used to resolve variables.
50       */
51      @Inject
52      private Injector injector;
53  
54      public String resolve(final String pattern) {
55      	StringBuilder buffer = new StringBuilder();
56      	
57          int prev = 0;
58          int pos;
59          while ((pos = pattern.indexOf(VAR_BEGIN, prev)) >= 0) {
60              if (pos > 0) {
61                  buffer.append(pattern.substring(prev, pos));
62              }
63              if (pos == pattern.length() - 1) {
64              	buffer.append(VAR_BEGIN);
65                  prev = pos + 1;
66              } else if (pattern.charAt(pos + 1) != '{') {
67                  if (pattern.charAt(pos + 1) == '$') {
68                  	buffer.append(VAR_BEGIN);
69                      prev = pos + 2;
70                  } else {
71                  	buffer.append(pattern.substring(pos, pos + 2));
72                      prev = pos + 2;
73                  }
74              } else {
75                  int endName = pattern.indexOf('}', pos);
76                  if (endName < 0) {
77                      throw new IllegalArgumentException("Syntax error in property: " + pattern);
78                  }
79                  StringTokenizer keyTokenizer = new StringTokenizer(pattern.substring(pos + 2, endName), PIPE_SEPARATOR);
80                  String key = keyTokenizer.nextToken().trim();
81                  String defaultValue = null;
82                  if (keyTokenizer.hasMoreTokens()) {
83                      defaultValue = keyTokenizer.nextToken().trim();
84                  }
85  
86                  try {
87                      buffer.append(injector.getInstance(Key.get(String.class, Names.named(key))));
88                  } catch (Throwable e) {
89                      if (defaultValue != null) {
90                          buffer.append(defaultValue);
91                      } else {
92                          buffer.append(KEY_PREFIX).append(key).append('}');
93                      }
94                  }
95  
96                  prev = endName + 1;
97              }
98          }
99          if (prev < pattern.length()) {
100             buffer.append(pattern.substring(prev));
101         }
102         
103         return buffer.toString();
104     }
105 
106     public static void main(String[] args) {
107 		Injector injector = Guice.createInjector(new AbstractModule() {
108 			@Override
109 			protected void configure() {
110 				bindConstant().annotatedWith(Names.named("variable.1")).to("feuer");
111 				bindConstant().annotatedWith(Names.named("variable.2")).to("frei");
112 				bindConstant().annotatedWith(Names.named("config.soap.protocol")).to("ftp");
113 				bindConstant().annotatedWith(Names.named("config.soap.ip")).to("1.1.1.1");
114 				bindConstant().annotatedWith(Names.named("config.soap.port")).to("9999");
115 				bindConstant().annotatedWith(Names.named("config.soap.app")).to("dynmaic");
116 				bindConstant().annotatedWith(Names.named("config.soap.client")).to("/henkel");
117 				bindConstant().annotatedWith(Names.named("config.soap.stage")).to("test");
118 			}
119 		});
120 		
121 		VariableResolver resolver = injector.getInstance(VariableResolver.class);
122 		System.out.println(resolver.resolve("${variable.1} ${variable.2}"));
123 		System.out.println(resolver.resolve("\"${variable.3| }\""));
124 		System.out.println(resolver.resolve("${config.soap.protocol|http}://${config.soap.ip|127.0.0.1}:${config.soap.port|12400}/${config.soap.app|configuration}${config.soap.client| }/soap/optional.xml?stage=${config.soap.stage|default}"));
125 	}
126 
127 }