View Javadoc

1   package net.sf.mmapps.modules.config;
2   import java.io.*;
3   
4   public class MMBaseFileConfigurationReader {
5   
6       /*** Creates a new instance of MMBaseFileConfigurationReader */
7       public MMBaseFileConfigurationReader() {
8       }
9   
10      public void readConfiguration(Configuration config, File dir) throws IOException {
11          if (dir.isDirectory()) {
12              File[] files = dir.listFiles();
13              for (int x = 0; x < files.length; x++) {
14                  File file = files[x];
15                  if (file.getName().equals("builders") && file.isDirectory()) {
16                      NodeManagerConfigurations configs = readNodeManagerDir(file,config);
17                      for (int y = 0; y < configs.size(); y++) {
18                          config.addNodeManagerConfiguration(configs.getNodeManagerConfiguration(y));
19                      }
20                   //   System.err.println(file.getName());
21                  }
22                  if (file.getName().equals("applications") && file.isDirectory()) {
23                      ApplicationConfigurations appConfigs = readApplicationDir(config,file);
24                      for (int y = 0; y < appConfigs.size(); y++) {
25                          config.addApplicationConfiguration(appConfigs.getApplicationConfiguration(y));
26                      }
27                  }
28              }
29          }
30      }
31  
32      public NodeManagerConfigurations readNodeManagerDir(File dir,Configuration config) throws IOException {
33          NodeManagerConfigurations nodeManagerConfigurations = new NodeManagerConfigurations();
34          File[] files = dir.listFiles();
35          for (int x = 0; x < files.length; x++) {
36              File file = files[x];
37  	    //read sub directories but skip the application builders
38              if (file.isDirectory() &&  ! file.getName().equals("applications")) {
39                  nodeManagerConfigurations.addAll(readNodeManagerDir(file,config));
40              } else if (file.isFile()) {
41                  if (file.getName().endsWith(".xml")) {
42                      NodeManagerConfiguration nodeManagerConfiguration =
43                          ConfigurationXMLReader.createNodeManagerConfiguration(config,readFile(file));
44  		    	if (nodeManagerConfiguration.getName() == null || nodeManagerConfiguration.getName().equals("")){
45  				System.err.println("detected old configuration.. converting");
46  				String name = file.getName().substring(0,file.getName().length() -4);
47  				nodeManagerConfiguration.setName(name);
48  				if (nodeManagerConfiguration.getFieldConfigurations().getFieldConfiguration("rnumber") != null){
49  					nodeManagerConfiguration.setExtends("insrel");
50  				} else {
51  					nodeManagerConfiguration.setExtends("object");
52  				}
53  				FieldConfigurations configs = nodeManagerConfiguration.getFieldConfigurations();
54  				for (int y = 0 ; y < configs.size() ; y ++){
55  					FieldConfiguration c = configs.getFieldConfiguration(y);
56  					String fieldname = c.getName();
57  					if (fieldname.equals("snumber") || fieldname.equals("dnumber") || fieldname.equals("rnumber") || fieldname.equals("dir") || fieldname.equals("number") || fieldname.equals("type") || fieldname.equals("owner")){
58  						configs.remove(y);
59  						y--;
60  					}
61  				}
62  			}
63                          nodeManagerConfigurations.add(nodeManagerConfiguration);
64                  }
65              }
66          }
67          return nodeManagerConfigurations;
68      }
69  
70      public ApplicationConfigurations readApplicationDir(Configuration config,File dir) throws IOException {
71          ApplicationConfigurations applicationConfigurations = new ApplicationConfigurations();
72          File[] files = dir.listFiles();
73          for (int x = 0; x < files.length; x++) {
74              File file = files[x];
75              if (file.isFile()) {
76                  if (file.getName().endsWith(".xml")) {
77                      ApplicationConfiguration applicationConfiguration =
78                      ConfigurationXMLReader.createApplicationConfiguration(config,readFile(file));
79                      applicationConfigurations.add(applicationConfiguration);
80  
81  		    File builderDir = new File(dir ,applicationConfiguration.getName() + File.separator  +"builders");
82  		    if (builderDir.exists() && builderDir.isDirectory()){
83  			     applicationConfiguration.getNodeManagerConfigurations().addAll(readNodeManagerDir(builderDir,config));
84  
85  		    } else {
86  			    System.err.println("missing builder dir for application " + applicationConfiguration.getName());
87  		    }
88                  }
89              }
90          }
91          return applicationConfigurations;
92      }
93  
94      public String readFile(File file) throws IOException {
95          BufferedReader br = new BufferedReader(new FileReader(file));
96          StringBuffer sb = new StringBuffer();
97          String line;
98          while ((line = br.readLine()) != null) {
99              sb.append(line);
100             sb.append("\n");
101         }
102         br.close();
103         return sb.toString();
104     }
105     
106     public static void main(String[] argv) throws IOException {
107         MMBaseFileConfigurationReader mmbaseFileConfigurationReader = new MMBaseFileConfigurationReader();
108         Configuration config = new Configuration();
109         mmbaseFileConfigurationReader.readConfiguration(config, new File("/home/keesj/config"));
110     }
111 
112 }