Project Report: fawkez

Packagesummary org.jcoderz.commons.connector.file

org.jcoderz.commons.connector.file.FsConnectionUtil

LineHitsNoteSource
1  /*
2   * $Id: FsConnectionUtil.java 1011 2008-06-16 17:57:36Z amandel $
3   *
4   * Copyright 2006, The jCoderZ.org Project. All rights reserved.
5   *
6   * Redistribution and use in source and binary forms, with or without
7   * modification, are permitted provided that the following conditions are
8   * met:
9   *
10   *    * Redistributions of source code must retain the above copyright
11   *      notice, this list of conditions and the following disclaimer.
12   *    * Redistributions in binary form must reproduce the above
13   *      copyright notice, this list of conditions and the following
14   *      disclaimer in the documentation and/or other materials
15   *      provided with the distribution.
16   *    * Neither the name of the jCoderZ.org Project nor the names of
17   *      its contributors may be used to endorse or promote products
18   *      derived from this software without specific prior written
19   *      permission.
20   *
21   * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND
22   * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24   * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS
25   * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28   * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29   * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30   * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32   */
33  package org.jcoderz.commons.connector.file;
34   
35  import java.rmi.RemoteException;
36  import java.util.Properties;
37  import java.util.logging.Level;
38  import java.util.logging.Logger;
39   
40  import javax.naming.InitialContext;
41  import javax.naming.NamingException;
42  import javax.resource.ResourceException;
43   
44  import org.jcoderz.commons.InternalErrorException;
45  import org.jcoderz.commons.RemoteCallFailureException;
46  import org.jcoderz.commons.connector.ConnectorConfiguration;
47  import org.jcoderz.commons.config.ConfigurationServiceContainerFactory;
48  import org.jcoderz.commons.config.ConfigurationServiceInterface;
49   
50   
51  /**
52   * This class provides utility functions for the file system connector.
53   *
54   * @author Michael Griffel
55   */
56  public final class FsConnectionUtil
57  {
58     /** The JNDI name of the file system connector. */
59     public static final String FILE_SYSTEM_CONNECTOR_JNDI_NAME
60           = "FileSystemConnector";
61   
62     /** The JNDI name of the file system connector. */
63     public static final String FILE_SYSTEM_CONNECTOR_EIS_NAME
64           = "java:comp/env/eis/FileSystemConnector";
65   
66     /** The full qualified name of this class. */
67     private static final String CLASSNAME
68           = FsConnectionUtil.class.getName();
69   
70     /** The logger to use. */
71     private static final Logger logger = Logger.getLogger(CLASSNAME);
72   
73     private static Properties sProps;
74     private static boolean sConfigRead;
75   
76     private FsConnectionUtil ()
77     {
78        // no instances allowed - provides only static helper methods.
79     }
80   
81     /**
82      * Returns a file system connection.
83      * @return a file system connection.
84      */
85     public static FsConnection getFileSystemConnection ()
86     {
87        return getFileSystemConnection(getConfigProperties());
88     }
89   
90     /**
91      * Returns a file system connection.
92      * @param props Connection properties to use
93      * @return a file system connection.
94      */
95     public static FsConnection getFileSystemConnection (final Properties props)
96     {
97        InitialContext context = null;
98        final FsConnection connection;
99        try
100        {
101           context = new InitialContext();
102           final FsConnectionFactory factory
103                 = (FsConnectionFactory) context.lookup(
104                    FILE_SYSTEM_CONNECTOR_EIS_NAME);
105           connection = factory.getConnection(props);
106        }
107        // possible exceptions: ResourceException, NamingException
108        catch (Exception e)
109        {
110 (1)         // CHECKME: catch ResourceException? provider connector specific ex.?
111 (2)         throw new InternalErrorException(
112                 "Failed to create file system connection", e);
113        }
114        finally
115        {
116           if (context != null)
117           {
118              try
119              {
120                 context.close();
121              }
122              catch (NamingException e)
123              {
124                 logger.log(Level.FINER,
125                       "Failed to close InitialContext: " + e, e);
126              }
127           }
128        }
129        return connection;
130     }
131   
132     /**
133      * Closes the file system connection (safe).
134      *
135      * This method tries to close the given file system connection and
136      * if an ResourceException occurs a message with the level
137      * {@link Level#FINE} is logged. It's safe to pass a
138      * <code>null</code> reference for the argument.
139      *
140      * @param in the file system connection that should be closed.
141      */
142     public static void close (FsConnection in)
143     {
144        if (in != null)
145        {
146           try
147           {
148              in.close();
149           }
150           catch (ResourceException x)
151           {
152              logger.log(Level.FINE, "Error while closing file "
153                    + "system connection: FsConnection.close()", x);
154           }
155        }
156     }
157   
158     /**
159      * Gets the connector configuration obtained from config service.
160      *
161      * @return ConnectorConfiguration providing getter methods for all
162      *          connector parameter values defined
163      */
164     static ConnectorConfiguration getConfiguration ()
165     {
166        final ConnectorConfiguration result;
167        final ConfigurationServiceInterface configService
168              = ConfigurationServiceContainerFactory.createLocalService();
169        try
170        {
171           result = (ConnectorConfiguration)
172                 configService.getServiceConfiguration(
173              "org.jcoderz.commons.connector.ConnectorConfiguration");
174        }
175        catch (RemoteException e)
176        {
177           throw new RemoteCallFailureException(e);
178        }
179        return result;
180     }
181   
182     private static synchronized Properties getConfigProperties ()
183     {
184        if (!sConfigRead)
185        {
186           final ConnectorConfiguration cc = getConfiguration();
187           final String tmpDir = cc.getFileTempDir();
188   
189           logger.fine("Using temp dir from config '" + tmpDir + "'.");
190 (3)         if (tmpDir != null && tmpDir.length() > 0 && !tmpDir.equals("_EMPTY_"))
191           {
192              sProps = new Properties();
193              sProps.put(FsConnectionFactory.PROP_TEMP_DIR, tmpDir);
194           }
195   
196           sConfigRead = true;
197        }
198   
199        return sProps;
200     }
201  }
202   

Findings in this File

i (1) 110 : 0 Comment matches to-do format '(TODO|FIXME|CHECKME)'.
i (2) 111 : 0 method org.jcoderz.commons.connector.file.FsConnectionUtil.getFileSystemConnection(Properties) throws exception with static message string
i (3) 190 : 0 method org.jcoderz.commons.connector.file.FsConnectionUtil.getConfigProperties() makes literal string comparisons passing the literal as an argument