| 1 | | | |
| 2 | | | |
| 3 | | | |
| 4 | | | |
| 5 | | | |
| 6 | | | |
| 7 | | | |
| 8 | | | |
| 9 | | | |
| 10 | | | |
| 11 | | | |
| 12 | | | |
| 13 | | | |
| 14 | | | |
| 15 | | | |
| 16 | | | |
| 17 | | | |
| 18 | | | |
| 19 | | | |
| 20 | | | |
| 21 | | | |
| 22 | | | |
| 23 | | | |
| 24 | | | |
| 25 | | | |
| 26 | | | |
| 27 | | | |
| 28 | | | |
| 29 | | | |
| 30 | | | |
| 31 | | | |
| 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 | | | |
| 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 | | | |
| 108 | | | catch (Exception e) |
| 109 | | | { |
| 110 | | (1) | |
| 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 | | | |