View Javadoc

1   /*
2   *
3   * ====================================================================
4   *
5   * Copyright 2004 The Apache Software Foundation 
6   *
7   * Licensed under the Apache License, Version 2.0 (the "License");
8   * you may not use this file except in compliance with the License.
9   * You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20  package org.apache.commons.contract.store;
21  
22  import org.apache.commons.contract.Context;
23  import org.apache.commons.contract.Store;
24  import org.apache.commons.contract.StoreException;
25  import org.apache.commons.contract.constraints.CastException;
26  import org.apache.commons.contract.constraints.StringConstraints;
27  import org.apache.commons.i18n.bundles.ErrorBundle;
28  
29  public class Environment implements Store {
30      public final static String ID = "environment";
31      
32      public void put(String key, Object value, Context context) throws StoreException {
33          try {
34              System.setProperty(key, (String)StringConstraints.UNCONSTRAINED.cast(value, context));
35          } catch (CastException e) {
36              throw new StoreException(new ErrorBundle("storesStringsOnly"), e);
37          }
38      }
39  
40      public Object get(String key, Context context) throws StoreException {
41          return System.getProperty(key);
42      }
43  
44      public void dispose(String key, Context context) throws StoreException {
45          throw new StoreException(new ErrorBundle("disposeNotAvailable"));
46      }
47      
48      public String getId() {
49          return ID;
50      }
51  }