View Javadoc

1   package org.apache.velocity.tools;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.ArrayList;
23  import java.util.Collections;
24  import java.util.List;
25  
26  /**
27   * A specialized constants class to provide some compile-time typo checking and
28   * runtime validation for scopes specified in annotations, toolbox configs, etc.
29   *
30   * @author Nathan Bubna
31   * @version $Id: Toolbox.java 511959 2007-02-26 19:24:39Z nbubna $
32   */
33  public final class Scope
34  {
35      public static final String REQUEST = "request";
36      public static final String SESSION = "session";
37      public static final String APPLICATION = "application";
38  
39      private static final List<String> VALUES;
40      static
41      {
42          List<String> defaults = new ArrayList<String>(3);
43          defaults.add(REQUEST);
44          defaults.add(SESSION);
45          defaults.add(APPLICATION);
46          VALUES = Collections.synchronizedList(defaults);
47      }
48  
49      // keep an instance available in case someone wants to
50      // drop this into a template for some reason
51      private static final Scope INSTANCE = new Scope();
52  
53      public static final Scope getInstance()
54      {
55          return INSTANCE;
56      }
57  
58      public static final void add(String newScope)
59      {
60          // keep everything lower case
61          newScope = newScope.toLowerCase();
62          // complain if it already exists
63          if (VALUES.contains(newScope))
64          {
65              throw new IllegalArgumentException("Scope '"+newScope+"' has already been registered.");
66          }
67          VALUES.add(newScope);
68      }
69  
70      public static final boolean exists(String scope)
71      {
72          // keep everything lower case
73          scope = scope.toLowerCase();
74          return VALUES.contains(scope);
75      }
76  
77      public static final List<String> values()
78      {
79          return Collections.unmodifiableList(VALUES);
80      }
81  
82      private Scope()
83      {
84          // keep constructor private, this is a singleton
85      }
86  
87  }