View Javadoc

1   /*
2    * Copyright 2007-2008 the original author or authors.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.gwe.app.client.config;
18  
19  import java.io.FileNotFoundException;
20  import java.io.IOException;
21  
22  import org.apache.commons.logging.Log;
23  import org.apache.commons.logging.LogFactory;
24  import org.gwe.persistence.model.GridInfo;
25  import org.gwe.persistence.model.HeadResourceInfo;
26  import org.gwe.persistence.model.VarInfo;
27  import org.gwe.utils.cmd.OptionParser;
28  import org.gwe.utils.security.CredentialNotFoundException;
29  import org.gwe.utils.security.KeyStore;
30  import org.gwe.utils.xstream.AliasTransf;
31  import org.gwe.utils.xstream.XMLConfigFile;
32  
33  /**
34   * @author Marco Ruiz
35   * @since Jan 28, 2008
36   */
37  public class XMLClientConfigReader implements ClientConfigReader {
38  
39  	private static Log log = LogFactory.getLog(XMLClientConfigReader.class);
40  
41  	public static final String CONF_ARG_PREFIX = "-conf=";
42  	
43  	public static final String DEFAULT_GRID_FILENAME = "gwe-grid.xml";
44  	private static final String DEFAULT_ENC_TOKEN    = "grid wizard enterprise";
45  
46  	private HeadResourceInfo headResource = null;
47  	private String daemonName = null;
48  	private String gridFN = DEFAULT_GRID_FILENAME;
49  	private String encryptionToken = DEFAULT_ENC_TOKEN;
50  
51  	private GridInfo grid;
52  	private KeyStore keys = null;
53  	
54  	public XMLClientConfigReader(String confArg) throws CredentialNotFoundException, IOException {
55  		InstallationFiles cfgFiles = ClientConfig.getInstallFiles();
56  
57  		// Format: -conf=[OPTIONAL_CLUSTER_NAME]@[OPTIONAL_CLUSTERS_FILENAME]:[OPTIONAL_KEYSTORE_FILENAME]
58  		OptionParser option = new OptionParser(CONF_ARG_PREFIX, confArg, '@', ':');
59  		daemonName      = option.getEle(0, daemonName);
60  		gridFN          = option.getEle(1, cfgFiles.getConfigFilePath(gridFN));
61  		encryptionToken = option.getEle(2, encryptionToken);
62  		
63  		grid = readGrid();
64  	}
65  
66  	private GridInfo readGrid() throws FileNotFoundException {
67  		AliasTransf<GridInfo>         at4Grid    = new AliasTransf<GridInfo>        (GridInfo.class,         "grid",    "headResources");
68  		AliasTransf<HeadResourceInfo> at4HeadRes = new AliasTransf<HeadResourceInfo>(HeadResourceInfo.class, "cluster", "vars");
69  		AliasTransf<VarInfo>          at4Var     = new AliasTransf<VarInfo>         (VarInfo.class,          "p2elVar");
70  		
71  		GridInfo grid = new XMLConfigFile<GridInfo>(gridFN, at4Grid, at4HeadRes, at4Var).getModel();
72  		headResource = grid.getHeadResource(daemonName);
73      	if (headResource == null && grid.getHeadResources().size() > 0) headResource = grid.getHeadResources().get(0);
74  	    return grid;
75      }
76  
77  	public GridInfo getGrid() {
78      	return grid;
79      }
80  
81  	public KeyStore getKeys() {
82  		if (keys == null) {
83  	        try {
84  	    		ConsoleKeyStorePasskeysReader reader = new ConsoleKeyStorePasskeysReader(ClientConfig.getInstallFiles(), encryptionToken, false);
85  				keys = reader.readKeyStore();
86  				keys.init();
87  	        } catch (IOException e) {
88  		        // TODO Auto-generated catch block
89  		        e.printStackTrace();
90  	        }
91  		}
92          return keys;
93      }
94  
95  	public HeadResourceInfo getHeadResource() {
96      	return headResource;
97      }
98  
99  	public static void main(String[] args) throws CredentialNotFoundException, IOException {
100 		new XMLClientConfigReader("-conf=@test-gwe-grid.xml:test-gwe-auth.xml");
101 	}
102 }
103