001 package org.apache.maven.scm.provider.starteam.util;
002
003 /*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements. See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership. The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License. You may obtain a copy of the License at
011 *
012 * http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied. See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022 import org.apache.maven.scm.providers.starteam.settings.Settings;
023 import org.apache.maven.scm.providers.starteam.settings.io.xpp3.StarteamXpp3Reader;
024 import org.codehaus.plexus.util.ReaderFactory;
025 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
026
027 import java.io.File;
028 import java.io.FileNotFoundException;
029 import java.io.IOException;
030
031 /**
032 * @author <a href="mailto:dantran@apache.org">Dan T. Tran</a>
033 * @version $Id: $
034 */
035 public final class StarteamUtil
036 {
037
038 protected static final String STARTEAM_SETTINGS_FILENAME = "starteam-settings.xml";
039
040 public static final File DEFAULT_SETTINGS_DIRECTORY = new File( System.getProperty( "user.home" ), ".scm" );
041
042 private static File settingsDirectory = DEFAULT_SETTINGS_DIRECTORY;
043
044 private static Settings settings;
045
046 private StarteamUtil()
047 {
048 }
049
050 public static Settings getSettings()
051 {
052 if ( settings == null )
053 {
054 settings = readSettings();
055 }
056 return settings;
057 }
058
059 public static Settings readSettings()
060 {
061 File settingsFile = getSettingsFile();
062
063 if ( settingsFile.exists() )
064 {
065 StarteamXpp3Reader reader = new StarteamXpp3Reader();
066 try
067 {
068 return reader.read( ReaderFactory.newXmlReader( settingsFile ) );
069 }
070 catch ( FileNotFoundException e )
071 {
072 // nop
073 }
074 catch ( IOException e )
075 {
076 // nop
077 }
078 catch ( XmlPullParserException e )
079 {
080 String message = settingsFile.getAbsolutePath() + " isn't well formed. SKIPPED." + e.getMessage();
081
082 System.err.println( message );
083 }
084 }
085
086 return new Settings();
087 }
088
089 public static File getSettingsFile()
090 {
091 return new File( settingsDirectory, STARTEAM_SETTINGS_FILENAME );
092 }
093
094
095 public static void setSettingsDirectory( File directory )
096 {
097 settingsDirectory = directory;
098 }
099 }