View Javadoc
1   package org.apache.maven.scm.provider.starteam.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 org.apache.maven.scm.providers.starteam.settings.Settings;
23  import org.apache.maven.scm.providers.starteam.settings.io.xpp3.StarteamXpp3Reader;
24  import org.codehaus.plexus.util.ReaderFactory;
25  import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
26  
27  import java.io.File;
28  import java.io.FileNotFoundException;
29  import java.io.IOException;
30  
31  /**
32   * @author <a href="mailto:dantran@apache.org">Dan T. Tran</a>
33   * @version $Id: $
34   */
35  public final class StarteamUtil
36  {
37  
38      protected static final String STARTEAM_SETTINGS_FILENAME = "starteam-settings.xml";
39  
40      public static final File DEFAULT_SETTINGS_DIRECTORY = new File( System.getProperty( "user.home" ), ".scm" );
41  
42      private static File settingsDirectory = DEFAULT_SETTINGS_DIRECTORY;
43  
44      private static Settings settings;
45  
46      private StarteamUtil()
47      {
48      }
49  
50      public static Settings getSettings()
51      {
52        if ( settings == null )
53        {
54      	  settings = readSettings();
55        }
56        return settings;
57      }
58      
59      public static Settings readSettings()
60      {
61          File settingsFile = getSettingsFile();
62  
63          if ( settingsFile.exists() )
64          {
65              StarteamXpp3Reader reader = new StarteamXpp3Reader();
66              try
67              {
68                  return reader.read( ReaderFactory.newXmlReader( settingsFile ) );
69              }
70              catch ( FileNotFoundException e )
71              {
72                  // nop
73              }
74              catch ( IOException e )
75              {
76                  // nop
77              }
78              catch ( XmlPullParserException e )
79              {
80                  String message = settingsFile.getAbsolutePath() + " isn't well formed. SKIPPED." + e.getMessage();
81  
82                  System.err.println( message );
83              }
84          }
85  
86          return new Settings();
87      }
88  
89      public static File getSettingsFile()
90      {
91         return new File( settingsDirectory, STARTEAM_SETTINGS_FILENAME );
92      }
93  
94      
95      public static void setSettingsDirectory( File directory )
96      {
97          settingsDirectory = directory;
98      }
99  }