View Javadoc

1   package org.apache.maven.scm.provider.clearcase.util;
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.io.File;
23  import java.io.FileNotFoundException;
24  import java.io.IOException;
25  import java.util.ResourceBundle;
26  
27  import org.apache.maven.scm.providers.clearcase.settings.Settings;
28  import org.apache.maven.scm.providers.clearcase.settings.io.xpp3.ClearcaseXpp3Reader;
29  import org.codehaus.plexus.util.ReaderFactory;
30  import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
31  
32  /**
33   * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
34   *
35   */
36  public final class ClearCaseUtil
37  {
38  
39      protected static final String CLEARCASE_SETTINGS_FILENAME = "clearcase-settings.xml";
40  
41      public static final File DEFAULT_SETTINGS_DIRECTORY = new File( System.getProperty( "user.home" ), ".scm" );
42  
43      private static File settingsDirectory = DEFAULT_SETTINGS_DIRECTORY;
44  
45      private static final String RESOURCE_FILENAME = "org.apache.maven.scm.provider.clearcase.command.clearcase";
46  
47      private static final ResourceBundle RESOURCE_BUNDLE = ResourceBundle.getBundle( RESOURCE_FILENAME );
48  
49      private static Settings settings;
50  
51      private ClearCaseUtil()
52      {
53      }
54  
55      public static String getLocalizedResource( String key )
56      {
57          return RESOURCE_BUNDLE.getString( key );
58      }
59  
60      public static Settings getSettings() 
61      {
62      	if ( settings == null )
63      	{
64      		settings = readSettings();
65      	}
66      	return settings;
67      }
68      
69      public static Settings readSettings() 
70      {
71          File settingsFile = new File( settingsDirectory, CLEARCASE_SETTINGS_FILENAME );
72  
73          if ( !settingsFile.exists() )
74          {
75              File scmGlobalDir = new File( System.getProperty( "maven.home" ), "conf" );
76              settingsFile = new File( scmGlobalDir, CLEARCASE_SETTINGS_FILENAME );
77          }
78  
79          if ( settingsFile.exists() )
80          {
81              ClearcaseXpp3Reader reader = new ClearcaseXpp3Reader();
82              try
83              {
84                  return reader.read( ReaderFactory.newXmlReader( settingsFile ) );
85              }
86              catch ( FileNotFoundException e )
87              {
88                  // nop
89              }
90              catch ( IOException e )
91              {
92                  // nop
93              }
94              catch ( XmlPullParserException e )
95              {
96                  String message = settingsFile.getAbsolutePath() + " isn't well formed. SKIPPED." + e.getMessage();
97  
98                  System.out.println( message );
99              }
100         }
101 
102         return new Settings();
103     }
104 
105     public static void setSettingsDirectory( File directory )
106     {
107         settingsDirectory = directory;
108         settings = readSettings();
109     }
110 }