| 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.util.Properties; |
| 36 | | | import java.util.logging.Level; |
| 37 | | | import java.util.logging.Logger; |
| 38 | | | |
| 39 | | | import javax.resource.ResourceException; |
| 40 | | | import javax.resource.spi.ConnectionManager; |
| 41 | | | import javax.resource.spi.ConnectionRequestInfo; |
| 42 | | | import javax.resource.spi.ManagedConnection; |
| 43 | | | |
| 44 | | | import org.jcoderz.commons.connector.ManagedConnectionFactoryBase; |
| 45 | | | import org.jcoderz.commons.connector.UserPassword; |
| 46 | | | |
| 47 | | | |
| 48 | | | /** |
| 49 | | | * Implements the {@link javax.resource.spi.ManagedConnectionFactory} interface. |
| 50 | | | * |
| 51 | | | */ |
| 52 | | (1) | public class FsManagedConnectionFactoryImpl |
| 53 | | | extends ManagedConnectionFactoryBase |
| 54 | | | { |
| 55 | | | /** Min value for chunk size, just 1KByte. */ |
| 56 | | | public static final long FILE_TRANSFER_CHUNK_SIZE_MIN_VALUE = 1000L; |
| 57 | | | /** Max value for chunk size, just {@link Integer#MAX_VALUE}. */ |
| 58 | | | public static final long FILE_TRANSFER_CHUNK_SIZE_MAX_VALUE |
| 59 | | | = Integer.MAX_VALUE; |
| 60 | | | |
| 61 | | | /** The full qualified name of this class. */ |
| 62 | | | private static final String CLASSNAME |
| 63 | | | = FsManagedConnectionFactoryImpl.class.getName(); |
| 64 | | | |
| 65 | | | /** The logger to use. */ |
| 66 | | | private static final Logger logger = Logger.getLogger(CLASSNAME); |
| 67 | | | |
| 68 | | | /** Generated <code>serialVersionUID</code>. */ |
| 69 | | | private static final long serialVersionUID = 3258409517263172660L; |
| 70 | | | |
| 71 | | | private final Properties mProps = new Properties(); |
| 72 | | | |
| 73 | | | protected ManagedConnection createManagedConnectionImpl (UserPassword up, |
| 74 | | | ConnectionRequestInfo cri) |
| 75 | | | { |
| 76 | | | final boolean finer = logger.isLoggable(Level.FINER); |
| 77 | | | if (finer) |
| 78 | | | { |
| 79 | | | logger.entering(CLASSNAME, "createManagedConnectionImpl", |
| 80 | | | new Object [] {up, cri}); |
| 81 | | | } |
| 82 | | | final ConnectionRequestInfo info; |
| 83 | | | |
| 84 | | | if (cri == null) |
| 85 | | | { |
| 86 | | | final FsConnectionRequestInfo fsCri = new FsConnectionRequestInfo(); |
| 87 | | | fsCri.setUserPassword(up); |
| 88 | | (2) | info = fsCri; |
| 89 | | | } |
| 90 | | | else |
| 91 | | | { |
| 92 | | | info = cri; |
| 93 | | | } |
| 94 | | | |
| 95 | | | final ManagedConnection result = new FsManagedConnectionImpl(this, up, |
| 96 | | (3) | cri); |
| 97 | | | |
| 98 | | | if (finer) |
| 99 | | | { |
| 100 | | | logger.exiting(CLASSNAME, "createManagedConnectionImpl", result); |
| 101 | | | } |
| 102 | | | return result; |
| 103 | | | } |
| 104 | | | |
| 105 | | | |
| 106 | | | protected boolean isMatchingManagedConnection (ManagedConnection mc, |
| 107 | | | UserPassword up, ConnectionRequestInfo cri) |
| 108 | | | throws ResourceException |
| 109 | | | { |
| 110 | | | final boolean finer = logger.isLoggable(Level.FINER); |
| 111 | | | if (finer) |
| 112 | | | { |
| 113 | | | logger.entering(CLASSNAME, "isMatchingManagedConnection", |
| 114 | | | new Object [] {mc, up, cri}); |
| 115 | | | } |
| 116 | | | boolean result = false; |
| 117 | | | |
| 118 | | | if ((mc instanceof FsManagedConnectionImpl) |
| 119 | | | && (cri instanceof FsConnectionRequestInfo)) |
| 120 | | | { |
| 121 | | | final FsManagedConnectionImpl mci = (FsManagedConnectionImpl) mc; |
| 122 | | | |
| 123 | | | if (mci.getUserPassword().equals(up)) |
| 124 | | | { |
| 125 | | | result = true; |
| 126 | | | } |
| 127 | | | } |
| 128 | | | |
| 129 | | | if (finer) |
| 130 | | | { |
| 131 | | | logger.exiting(CLASSNAME, "isMatchingManagedConnection", |
| 132 | | | Boolean.valueOf(result)); |
| 133 | | | } |
| 134 | | | |
| 135 | | | return result; |
| 136 | | | } |
| 137 | | | |
| 138 | | | |
| 139 | | | /** |
| 140 | | | * Creates a new FsConnectionFactoryImpl instance. |
| 141 | | | * @param cm The ConectionManager to use. |
| 142 | | | * @throws ResourceException thrown in error cases. |
| 143 | | | * |
| 144 | | | * @see org.jcoderz.commons.connector.ManagedConnectionFactoryBase#createConnectionFactoryImpl(javax.resource.spi.ConnectionManager) |
| 145 | | | */ |
| 146 | | | protected Object createConnectionFactoryImpl (ConnectionManager cm) |
| 147 | | | throws ResourceException |
| 148 | | | { |
| 149 | | | final boolean finer = logger.isLoggable(Level.FINER); |
| 150 | | | if (finer) |
| 151 | | | { |
| 152 | | | logger.entering(CLASSNAME, "createConnectionFactoryImpl", cm); |
| 153 | | | } |
| 154 | | | |
| 155 | | | checkProps(); |
| 156 | | | final FsConnectionFactoryImpl result = new FsConnectionFactoryImpl(cm, |
| 157 | | | this, mProps); |
| 158 | | | if (finer) |
| 159 | | | { |
| 160 | | | logger.exiting(CLASSNAME, "createConnectionFactoryImpl", result); |
| 161 | | | } |
| 162 | | | return result; |
| 163 | | | } |
| 164 | | | |
| 165 | | | private void checkProps () |
| 166 | | | { |
| 167 | | | if (!mProps.containsKey( |
| 168 | | | FsConnectionFactory.PROP_FILE_TRANSFER_CHUNK_SIZE)) |
| 169 | | | { |
| 170 | | | mProps.setProperty(FsConnectionFactory.PROP_FILE_TRANSFER_CHUNK_SIZE, |
| 171 | | | String.valueOf( |
| 172 | | | FsConnectionFactory.FILE_TRANSFER_CHUNK_SIZE_DEF_VALUE)); |
| 173 | | | } |
| 174 | | | } |
| 175 | | | |
| 176 | | | /** |
| 177 | | | * Sets the chunk size used while file transfering. |
| 178 | | | * If a file size exceeds the chunk size, the file will be transfered chunk |
| 179 | | | * by chunk until all bytes will have been transfered. The Min value for this |
| 180 | | | * property is |
| 181 | | | * {@link FsManagedConnectionFactoryImpl#FILE_TRANSFER_CHUNK_SIZE_MIN_VALUE}, |
| 182 | | | * the Max value is |
| 183 | | | * {@link FsManagedConnectionFactoryImpl#FILE_TRANSFER_CHUNK_SIZE_MAX_VALUE}. |
| 184 | | | * If this property is not defined in the deployment descriptor the file |
| 185 | | | * connector will use the default value |
| 186 | | | * {@linkplain FsConnectionFactory#FILE_TRANSFER_CHUNK_SIZE_DEF_VALUE}. |
| 187 | | | * Too small value will probably slow the performance down, and too large |
| 188 | | | * value may cause a resource's allocation problem. The value of this |
| 189 | | | * property should be adjusted to the underlying os, file system and |
| 190 | | | * available memory. |
| 191 | | | * A connection client can overwrite this value by passing a property |
| 192 | | | * object while retrieving a connection by calling the method |
| 193 | | | * {@link FsConnectionFactory#getConnection(Properties)}. |
| 194 | | | * |
| 195 | | | * @param size the new chunk size to set. |
| 196 | | | */ |
| 197 | | | public void setFileTransferChunkSize (Long size) |
| 198 | | | { |
| 199 | | | final String method = "setFileTransferChunkSize"; |
| 200 | | | logger.entering(CLASSNAME, method, size); |
| 201 | | | |
| 202 | | | final String usedSize; |
| 203 | | | if (size.longValue() < FILE_TRANSFER_CHUNK_SIZE_MIN_VALUE |
| 204 | | | || size.longValue() > FILE_TRANSFER_CHUNK_SIZE_MAX_VALUE) |
| 205 | | | { |
| 206 | | | if (logger.isLoggable(Level.FINE)) |
| 207 | | | { |
| 208 | | | logger.fine("The given file transfer chunk " + size |
| 209 | | | + " could not be set. The chunk size should be between " |
| 210 | | | + FILE_TRANSFER_CHUNK_SIZE_MIN_VALUE + " and " |
| 211 | | | + FILE_TRANSFER_CHUNK_SIZE_MAX_VALUE |
| 212 | | | + ". The file connector wiil use the default chunk size " |
| 213 | | | + FsConnectionFactory.FILE_TRANSFER_CHUNK_SIZE_DEF_VALUE); |
| 214 | | | } |
| 215 | | | usedSize = String.valueOf( |
| 216 | | | FsConnectionFactory.FILE_TRANSFER_CHUNK_SIZE_DEF_VALUE); |
| 217 | | | } |
| 218 | | | else |
| 219 | | | { |
| 220 | | | usedSize = size.toString(); |
| 221 | | | } |
| 222 | | | |
| 223 | | | mProps.setProperty(FsConnectionFactory.PROP_FILE_TRANSFER_CHUNK_SIZE, |
| 224 | | | String.valueOf(usedSize)); |
| 225 | | | |
| 226 | | | logger.exiting(CLASSNAME, method); |
| 227 | | | } |
| 228 | | | |
| 229 | | | /** |
| 230 | | | * Defines the temporary directory to be used by this connector. The default |
| 231 | | | * value will be retrieved from the system property 'java.io.tmpdir'. |
| 232 | | | * A connection client can overwrite this value by passing the property |
| 233 | | | * {@linkplain FsConnectionFactory#PROP_TEMP_DIR} while retrieving a |
| 234 | | | * connection by calling the method |
| 235 | | | * {@link FsConnectionFactory#getConnection(Properties)}. |
| 236 | | | * |
| 237 | | | * @param dir temp dir to set. |
| 238 | | | */ |
| 239 | | | public void setTempDir (String dir) |
| 240 | | | { |
| 241 | | | logger.entering(CLASSNAME, "setTempDir", dir); |
| 242 | | | mProps.setProperty(FsConnectionFactory.PROP_TEMP_DIR, dir); |
| 243 | | | logger.exiting(CLASSNAME, "setTempDir"); |
| 244 | | | } |
| 245 | | | } |
| 246 | | | |