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   */
34  public final class StarteamUtil
35  {
36  
37      protected static final String STARTEAM_SETTINGS_FILENAME = "starteam-settings.xml";
38  
39      public static final File DEFAULT_SETTINGS_DIRECTORY = new File( System.getProperty( "user.home" ), ".scm" );
40  
41      private static File settingsDirectory = DEFAULT_SETTINGS_DIRECTORY;
42  
43      private static Settings settings;
44  
45      private StarteamUtil()
46      {
47      }
48  
49      public static Settings getSettings()
50      {
51          if ( settings == null )
52          {
53              settings = readSettings();
54          }
55          return settings;
56      }
57      
58      public static Settings readSettings()
59      {
60          File settingsFile = getSettingsFile();
61  
62          if ( settingsFile.exists() )
63          {
64              StarteamXpp3Reader reader = new StarteamXpp3Reader();
65              try
66              {
67                  return reader.read( ReaderFactory.newXmlReader( settingsFile ) );
68              }
69              catch ( IOException e )
70              {
71                  // nop
72              }
73              catch ( XmlPullParserException e )
74              {
75                  String message = settingsFile.getAbsolutePath() + " isn't well formed. SKIPPED." + e.getMessage();
76  
77                  System.err.println( message );
78              }
79          }
80  
81          return new Settings();
82      }
83  
84      public static File getSettingsFile()
85      {
86         return new File( settingsDirectory, STARTEAM_SETTINGS_FILENAME );
87      }
88  
89      
90      public static void setSettingsDirectory( File directory )
91      {
92          settingsDirectory = directory;
93      }
94  }