View Javadoc

1   package org.lsst.ccs.config;
2   
3   import org.lsst.ccs.bus.MessagingFactory;
4   import org.lsst.ccs.config.utilities.ConfigUtils;
5   import org.lsst.ccs.framework.ConfigurationProxy;
6   import org.lsst.ccs.utilities.structs.ViewValue;
7   import org.lsst.gruth.types.TypeUtils;
8   
9   import java.io.IOException;
10  import java.io.PrintWriter;
11  import org.lsst.ccs.bootstrap.BootstrapUtils;
12  import org.lsst.ccs.bootstrap.resources.BootstrapResourceUtils;
13  import org.lsst.ccs.utilities.logging.Logger;
14  
15  /**
16   * @author bamade
17   */
18  // Date: 04/10/12
19  
20  public class LocalConfigurationProxy implements ConfigurationProxy {
21      private ASubsystemDescription subsystemDescription ;
22      /**
23       * the reference configuration that is running in normal mode
24       */
25      private AConfigProfile baseProfile ;
26      /**
27       * the configuration that is running in engineering/normal mode
28       * (in normal mode both configurations are the same)
29       */
30      private AConfigProfile currentProfile ;
31  
32      private volatile boolean inEngineeringMode ;
33  
34      private MessagingFactory fac ;
35  
36      Logger logger = Logger.getLogger("org.lsst.ccs.config");
37  
38      //TODO : ctors
39  
40      public LocalConfigurationProxy(ConfigProfile configProfile) {
41          baseProfile = (AConfigProfile) configProfile ;
42          subsystemDescription = baseProfile.getSubsystemDesc() ;
43      }
44  
45      @Override
46      public synchronized String getConfigurationName() {
47          return currentProfile.getName() ;
48      }
49  
50      @Override
51      public synchronized String getTagName() {
52          return currentProfile.getTag() ;
53      }
54  
55      @Override
56      public synchronized void startNewConfigurationContext() {
57          if(inEngineeringMode) {
58              throw new IllegalStateException("already in engineering mode") ;
59          }
60          inEngineeringMode = true ;
61          // copy the configProfile and put it in engineering mode
62          currentProfile = new AConfigProfile(baseProfile, baseProfile.getName(),
63                  baseProfile.getTag(), "", PackCst.DESIGNER_LEVEL, true) ;
64          // currentProfile = voir factories
65      }
66  
67      @Override
68      public ViewValue checkForParameterChange(String componentName, String parameterName, Object value) {
69          String strValue ;
70          if(value instanceof String){
71             strValue = (String)  value ;
72          } else {
73              strValue = TypeUtils.stringify(value) ;
74          }
75          // check for engineering mode and create new
76          if(! inEngineeringMode) {
77              startNewConfigurationContext();
78          }
79          ParameterPath path = new ParameterPath(componentName,"",parameterName);
80          ParameterDescription parameterDescription = subsystemDescription.fetch(path) ;
81          if(null == parameterDescription) {
82              throw new IllegalArgumentException("incoherent parameter name for " + parameterName + "-> "
83              //+ subsystemDescription.getParamDescriptions());
84              + subsystemDescription.getSubsystemName());
85          }
86          if(parameterDescription.isNotModifiableAtRuntime()) {
87              throw new IllegalStateException(" parameter " + parameterName + " not modifiable at runtime");
88          }
89           Object res = parameterDescription.checkValue(strValue) ;
90          return new ViewValue(strValue, res) ;
91      }
92  
93      @Override
94      public synchronized void notifyParameterChange(String componentName, String parameterName, String value) {
95          ParameterPath path = new ParameterPath(componentName,"",parameterName);
96          //if static
97          currentProfile.temporaryChangeConfigurationValue(path.toString(), System.currentTimeMillis(), value);
98      }
99  
100     @Override
101     public synchronized void notifyUncheckedParameterChange(String componentName, String parameterName, Object value) {
102         ParameterPath path = new ParameterPath(componentName,"",parameterName);
103         String strValue ;
104         if( value instanceof String) {
105            strValue = (String)  value ;
106         } else {
107             strValue = TypeUtils.stringify(value) ;
108         }
109         //TODO : warn !!!
110         currentProfile.temporaryChangeConfigurationValue(path.toString(), System.currentTimeMillis(), strValue);
111     }
112 
113     @Override
114     public synchronized  void  registerConfiguration(String configurationName, String tagName) throws IOException{
115         if(!inEngineeringMode) {
116             return ;
117         }
118         NamesAndTag namesAndTag = new NamesAndTag(subsystemDescription.getSubsystemName(),
119                 configurationName,tagName) ;
120         // copy from engineering mode to normal
121         baseProfile = new AConfigProfile(currentProfile, currentProfile.getName(),
122                 currentProfile.getTag(), "", PackCst.DESIGNER_LEVEL, false) ;
123         currentProfile = baseProfile.clone() ;
124         String baseName = ConfigUtils.baseNameFromNames(namesAndTag);
125 
126         //Check if the properties file already exists in the Bootstrap Environment
127         String pathInBootstrap = BootstrapResourceUtils.getPathOfPropertiesFileInUserResourceDirectories(baseName);
128         //If it does not exist...
129         if ( pathInBootstrap == null ) {
130             // then check if a there is a Bootstrap user defined directory in which to write it.
131             String topMostUserDirectory = BootstrapResourceUtils.getTopUserResourceDirectory();
132             //If there is not Bootstrap user Directory defined.....
133             if ( topMostUserDirectory == null ) {
134                 //Set the path to org.lsst.ccs.workdir
135                 String workdir = BootstrapResourceUtils.getBootstrapSystemProperties().getProperty("org.lsst.ccs.workdir", "");
136                 if ( !workdir.isEmpty() && ! workdir.endsWith(BootstrapUtils.FILE_SEPARATOR)) {
137                     workdir += BootstrapUtils.FILE_SEPARATOR;
138                 }
139                 baseName = workdir+baseName;
140             } else {
141                 baseName = topMostUserDirectory+baseName;
142             }
143         } else {
144             baseName = pathInBootstrap;
145         }
146         
147         logger.info("Saving configuration tag "+tagName+" to "+baseName+".properties");
148         
149         
150         PrintWriter printWriter = new PrintWriter(baseName+".properties", "UTF-8") ;
151         currentProfile.generateConfigProperties(printWriter);
152         // make subsystem send change!
153         inEngineeringMode = false ;
154         printWriter.flush() ;
155         printWriter.close() ;
156     }
157 
158     @Override
159     public synchronized void dropModifications() {
160         currentProfile =  baseProfile.clone() ;
161         // make subsystem change notification
162         inEngineeringMode = false ;
163     }
164 
165     @Override
166     public Object getDefaultParameterValue(String componentName, String parameterName) {
167         ParameterPath path = new ParameterPath(componentName,"",parameterName);
168         ParameterDescription parameterDescription = subsystemDescription.fetch(path) ;
169 
170         String strValue = parameterDescription.getDefaultValue();
171         String type = parameterDescription.getTypeName() ;
172         Object res = Constraints.check(type,strValue, null) ;
173         return res;
174     }
175 
176     @Override
177     public void setMessagingFactory(MessagingFactory factory) {
178         this.fac = factory ;
179     }
180 }