001    package org.apache.maven.scm.provider.clearcase.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 java.io.File;
023    import java.io.FileNotFoundException;
024    import java.io.IOException;
025    import java.util.ResourceBundle;
026    
027    import org.apache.maven.scm.providers.clearcase.settings.Settings;
028    import org.apache.maven.scm.providers.clearcase.settings.io.xpp3.ClearcaseXpp3Reader;
029    import org.codehaus.plexus.util.ReaderFactory;
030    import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
031    
032    /**
033     * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
034     * @version $Id: ClearCaseUtil.java 1134992 2011-06-12 21:54:27Z godin $
035     */
036    public final class ClearCaseUtil
037    {
038    
039        protected static final String CLEARCASE_SETTINGS_FILENAME = "clearcase-settings.xml";
040    
041        public static final File DEFAULT_SETTINGS_DIRECTORY = new File( System.getProperty( "user.home" ), ".scm" );
042    
043        private static File settingsDirectory = DEFAULT_SETTINGS_DIRECTORY;
044    
045        private static final String RESOURCE_FILENAME = "org.apache.maven.scm.provider.clearcase.command.clearcase";
046    
047        private static final ResourceBundle RESOURCE_BUNDLE = ResourceBundle.getBundle( RESOURCE_FILENAME );
048    
049        private static Settings settings;
050    
051        private ClearCaseUtil()
052        {
053        }
054    
055        public static String getLocalizedResource( String key )
056        {
057            return RESOURCE_BUNDLE.getString( key );
058        }
059    
060        public static Settings getSettings() 
061        {
062            if ( settings == null )
063            {
064                    settings = readSettings();
065            }
066            return settings;
067        }
068        
069        public static Settings readSettings() 
070        {
071            File settingsFile = new File( settingsDirectory, CLEARCASE_SETTINGS_FILENAME );
072    
073            if ( !settingsFile.exists() )
074            {
075                File scmGlobalDir = new File( System.getProperty( "maven.home" ), "conf" );
076                settingsFile = new File( scmGlobalDir, CLEARCASE_SETTINGS_FILENAME );
077            }
078    
079            if ( settingsFile.exists() )
080            {
081                ClearcaseXpp3Reader reader = new ClearcaseXpp3Reader();
082                try
083                {
084                    return reader.read( ReaderFactory.newXmlReader( settingsFile ) );
085                }
086                catch ( FileNotFoundException e )
087                {
088                    // nop
089                }
090                catch ( IOException e )
091                {
092                    // nop
093                }
094                catch ( XmlPullParserException e )
095                {
096                    String message = settingsFile.getAbsolutePath() + " isn't well formed. SKIPPED." + e.getMessage();
097    
098                    System.out.println( message );
099                }
100            }
101    
102            return new Settings();
103        }
104    
105        public static void setSettingsDirectory( File directory )
106        {
107            settingsDirectory = directory;
108            settings = readSettings();
109        }
110    }