- Timestamp:
- 10/25/14 21:35:19 (10 years ago)
- Location:
- SRUClient/trunk/src
- Files:
-
- 1 added
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
SRUClient/trunk/src/main/java/eu/clarin/sru/client/SRUClient.java
r2942 r5748 19 19 import java.util.ArrayDeque; 20 20 import java.util.Deque; 21 import java.util.HashMap;22 21 import java.util.LinkedList; 23 22 import java.util.List; 24 import java.util.Map;25 23 import java.util.concurrent.TimeUnit; 26 24 … … 77 75 78 76 /** 79 * Constructor. This constructor will create a <em>strict</em> client and 80 * use the default SRU version. 81 * 82 * @see SRUSimpleClient#DEFAULT_SRU_VERSION 77 * Constructor. 78 * 79 * @param config 80 * the configuration to be used for this client. 81 * @throws NullPointerException 82 * if argument <code>config</code> is <node>null</code> 83 * @throws IllegalArgumentException 84 * if an error occurred while registering record data parsers 85 * @see SRUClientConfig 83 86 */ 84 public SRUClient() { 85 this(SRUSimpleClient.DEFAULT_SRU_VERSION, 86 new HashMap<String, SRURecordDataParser>(), 87 DocumentBuilderFactory.newInstance()); 88 } 89 90 91 /** 92 * Constructor. This constructor will create a <em>strict</em> client. 93 * 94 * @param defaultVersion 95 * the default version to use for SRU requests; may be overridden 96 * by individual requests 97 */ 98 public SRUClient(SRUVersion defaultVersion) { 99 this(defaultVersion, 100 new HashMap<String, SRURecordDataParser>(), 101 DocumentBuilderFactory.newInstance()); 87 public SRUClient(final SRUClientConfig config) { 88 this(config, DocumentBuilderFactory.newInstance()); 102 89 } 103 90 … … 120 107 * Builders 121 108 */ 122 SRUClient(SRUVersion defaultVersion, 123 Map<String, SRURecordDataParser> parsers, 124 DocumentBuilderFactory documentBuilderFactory) { 125 if (defaultVersion == null) { 126 throw new NullPointerException("version == null"); 127 } 128 if (parsers == null) { 129 throw new NullPointerException("parsers == null"); 109 SRUClient(final SRUClientConfig config, 110 final DocumentBuilderFactory documentBuilderFactory) { 111 if (config == null) { 112 throw new NullPointerException("config == null"); 130 113 } 131 114 if (documentBuilderFactory == null) { 132 115 throw new NullPointerException("documentBuilderFactory == null"); 133 116 } 134 this.client = new SRUSimpleClient( defaultVersion, parsers);117 this.client = new SRUSimpleClient(config); 135 118 this.handler = new Handler(); 136 119 try { … … 145 128 } 146 129 reset(); 147 }148 149 150 /**151 * Register a record data parser.152 *153 * @param parser154 * a parser instance155 * @throws NullPointerException156 * if any required argument is <code>null</code>157 * @throws IllegalArgumentException158 * if the supplied parser is invalid or a parser handing the159 * same record schema is already registered160 */161 public void registerRecordParser(SRURecordDataParser parser) {162 client.registerRecordParser(parser);163 130 } 164 131 -
SRUClient/trunk/src/main/java/eu/clarin/sru/client/SRUSimpleClient.java
r5743 r5748 69 69 private static final String USER_AGENT = "SRU-Client/1.0.0"; 70 70 /** default version the client will use, if not otherwise specified */ 71 public static final SRUVersion DEFAULT_SRU_VERSION = SRUVersion.VERSION_1_2;72 71 private static final String SRU_NS = 73 72 "http://www.loc.gov/zing/srw/"; … … 89 88 new SRUExplainRecordDataParser(); 90 89 91 /**92 * Constructor. This constructor will create a <em>strict</em> client and93 * use the default SRU version.94 *95 * @see #DEFAULT_SRU_VERSION96 */97 public SRUSimpleClient() {98 this(DEFAULT_SRU_VERSION, new HashMap<String, SRURecordDataParser>());99 }100 101 102 /**103 * Constructor. This constructor will create a <em>strict</em> client.104 *105 * @param defaultVersion106 * the default version to use for SRU requests; may be overridden107 * by individual requests108 */109 public SRUSimpleClient(SRUVersion defaultVersion) {110 this(defaultVersion, new HashMap<String, SRURecordDataParser>());111 }112 113 90 114 91 /** 115 92 * Constructor. 116 93 * 117 * <p> 118 * For internal use only. 119 * </p> 120 * 121 * @param defaultVersion 122 * the default version to use for SRU requests; may be overridden 123 * by individual requests 124 * @param strictMode 125 * if <code>true</code> the client will strictly adhere to the 126 * SRU standard and raise fatal errors on violations, if 127 * <code>false</code> it will act more forgiving and ignore 128 * certain violations 129 * @param parsers 130 * a <code>Map</code> to store record schema to record data 131 * parser mappings 94 * @param config 95 * the configuration to be used for this client. 96 * @throws NullPointerException 97 * if argument <code>config</code> is <node>null</code> 98 * @throws IllegalArgumentException 99 * if an error occurred while registering record data parsers 100 * @see SRUClientConfig 132 101 */ 133 SRUSimpleClient(SRUVersion defaultVersion, 134 Map<String, SRURecordDataParser> parsers) { 135 if (defaultVersion == null) { 136 throw new NullPointerException("version == null"); 137 } 138 if (parsers == null) { 139 throw new NullPointerException("parsers == null"); 140 } 141 this.defaultVersion = defaultVersion; 142 this.parsers = parsers; 102 public SRUSimpleClient(final SRUClientConfig config) { 103 if (config == null) { 104 throw new NullPointerException("config == null"); 105 } 106 this.defaultVersion = config.getDefaultVersion(); 107 108 // Initialize parsers lookup table ... 109 final List<SRURecordDataParser> list = config.getRecordDataParsers(); 110 if ((list == null) || list.isEmpty()) { 111 throw new IllegalArgumentException( 112 "no record data parsers registered"); 113 } 114 this.parsers = new HashMap<String, SRURecordDataParser>(); 115 for (SRURecordDataParser parser : list) { 116 final String recordSchema = parser.getRecordSchema(); 117 if (!parsers.containsKey(recordSchema)) { 118 parsers.put(recordSchema, parser); 119 } else { 120 throw new IllegalArgumentException( 121 "record data parser already registered: " + 122 recordSchema); 123 } 124 } 143 125 144 126 // create HTTP client 145 // FIXME: get timeout values from somewhere? 146 final int connectTimeout = 30 * 1000; 147 final int socketTimeout = 180 * 1000; 148 httpClient = createHttpClient(connectTimeout, socketTimeout); 149 } 150 151 152 /** 153 * Register a record data parser. 154 * 155 * @param parser 156 * a parser instance 157 * @throws NullPointerException 158 * if any required argument is <code>null</code> 159 * @throws IllegalArgumentException 160 * if the supplied parser is invalid or a parser handing the 161 * same record schema is already registered 162 */ 163 public void registerRecordParser(SRURecordDataParser parser) { 164 if (parser == null) { 165 throw new NullPointerException("parser == null"); 166 } 167 final String recordSchema = parser.getRecordSchema(); 168 if (recordSchema == null) { 169 throw new NullPointerException("parser.getRecordSchema() == null"); 170 } 171 if (recordSchema.isEmpty()) { 172 throw new IllegalArgumentException( 173 "parser.getRecordSchema() returns empty string"); 174 } 175 176 if (!parsers.containsKey(recordSchema)) { 177 parsers.put(recordSchema, parser); 178 } else { 179 throw new IllegalArgumentException( 180 "record data parser already registered: " + recordSchema); 181 } 127 httpClient = createHttpClient(config.getConnectTimeout(), 128 config.getSocketTimeout()); 182 129 } 183 130 -
SRUClient/trunk/src/main/java/eu/clarin/sru/client/SRUThreadedClient.java
r2942 r5748 18 18 19 19 import java.util.concurrent.Callable; 20 import java.util.concurrent.ConcurrentHashMap;21 import java.util.concurrent.ConcurrentMap;22 20 import java.util.concurrent.ExecutorService; 23 21 import java.util.concurrent.Executors; … … 43 41 private static final Logger logger = 44 42 LoggerFactory.getLogger(SRUThreadedClient.class); 45 private final ConcurrentMap<String, SRURecordDataParser> parsers =46 new ConcurrentHashMap<String, SRURecordDataParser>();47 43 private final DocumentBuilderFactory documentBuilderFactory = 48 44 DocumentBuilderFactory.newInstance(); … … 52 48 53 49 /** 54 * Constructor. This constructor will create a <em>strict</em> client and55 * use the default SRU version.56 *57 * @see SRUSimpleClient#DEFAULT_SRU_VERSION58 */59 public SRUThreadedClient() {60 this(SRUSimpleClient.DEFAULT_SRU_VERSION);61 }62 63 64 /**65 50 * Constructor. 66 51 * 67 * @param defaultVersion 68 * the default version to use for SRU requests; may be overridden 69 * by individual requests 70 */ 71 public SRUThreadedClient(final SRUVersion defaultVersion) { 52 * @param config 53 * the configuration to be used for this client. 54 * @throws NullPointerException 55 * if argument <code>config</code> is <node>null</code> 56 * @throws IllegalArgumentException 57 * if an error occurred while registering record data parsers 58 * @see SRUClientConfig 59 */ 60 public SRUThreadedClient(final SRUClientConfig config) { 72 61 client = new ThreadLocal<SRUClient>() { 73 62 @Override 74 63 protected SRUClient initialValue() { 75 64 logger.debug("instantiated new sru client"); 76 return new SRUClient(defaultVersion, parsers, 77 documentBuilderFactory); 65 return new SRUClient(config, documentBuilderFactory); 78 66 } 79 67 }; 80 // TODO: make worker count configurable 81 int workerCount = Runtime.getRuntime().availableProcessors() * 2; 82 logger.debug("using {} workers", workerCount); 83 executor = Executors.newFixedThreadPool(workerCount, new Factory()); 84 } 85 86 87 /** 88 * Register a record data parser. 89 * 90 * @param parser 91 * a parser instance 92 * @throws NullPointerException 93 * if any required argument is <code>null</code> 94 * @throws IllegalArgumentException 95 * if the supplied parser is invalid or a parser handing the 96 * same record schema is already registered 97 */ 98 public void registerRecordParser(SRURecordDataParser parser) { 99 if (parser == null) { 100 throw new NullPointerException("parser == null"); 101 } 102 final String recordSchema = parser.getRecordSchema(); 103 if (recordSchema == null) { 104 throw new NullPointerException("parser.getRecordSchema() == null"); 105 } 106 if (recordSchema.isEmpty()) { 107 throw new IllegalArgumentException( 108 "parser.getRecordSchema() returns empty string"); 109 } 110 111 if (parsers.putIfAbsent(recordSchema, parser) != null) { 112 throw new IllegalArgumentException( 113 "record data parser already registered: " + recordSchema); 114 115 } 68 69 // launch workers ... 70 final int threadCount = config.getThreadCount(); 71 logger.debug("using {} workers", threadCount); 72 executor = Executors.newFixedThreadPool(threadCount, new Factory()); 116 73 } 117 74 -
SRUClient/trunk/src/main/java/eu/clarin/sru/client/fcs/ClarinFCSClientBuilder.java
r5743 r5748 5 5 6 6 import eu.clarin.sru.client.SRUClient; 7 import eu.clarin.sru.client.SRUClientConfig; 7 8 import eu.clarin.sru.client.SRUSimpleClient; 8 9 import eu.clarin.sru.client.SRUThreadedClient; … … 10 11 11 12 13 /** 14 * A class that implements the builder pattern for creating SRU client instances 15 * that are configured to be used for CLARIN-FCS. 16 * 17 */ 12 18 public class ClarinFCSClientBuilder { 13 19 private static final boolean DEFAULT_UNKNOWN_AS_DOM = … … 16 22 SRUVersion.VERSION_1_2; 17 23 private List<DataViewParser> parsers = new ArrayList<DataViewParser>(); 18 private SRUVersion defaultSruVersion = DEFAULT_SRU_VERSION; 19 private boolean unknownAsDom; 20 private boolean legacySupport = false; 24 private SRUVersion defaultVersion = DEFAULT_SRU_VERSION; 25 private boolean unknownAsDom = DEFAULT_UNKNOWN_AS_DOM; 26 private boolean legacySupport = false; 27 private int connectTimeout = SRUClientConfig.DEFAULT_CONNECT_TIMEOUT; 28 private int socketTimeout = SRUClientConfig.DEFAULT_SOCKET_TIMEOUT; 21 29 22 30 … … 37 45 38 46 39 public ClarinFCSClientBuilder defaults() { 47 /** 48 * Add the recommended default set of data record view parsers. 49 * 50 * @return this {@link ClarinFCSClientBuilder} instance 51 */ 52 public ClarinFCSClientBuilder addDefaultDataViewParsers() { 40 53 doRegisterDataViewParser(parsers, new DataViewParserHits()); 41 54 return this; … … 43 56 44 57 45 public ClarinFCSClientBuilder unkownDataViewAsDOM() { 58 /** 59 * Parse unknown data views into a DOM. 60 * 61 * @return this {@link ClarinFCSClientBuilder} instance 62 * @see DataViewParserGenericDOM 63 */ 64 public ClarinFCSClientBuilder unknownDataViewAsDOM() { 46 65 unknownAsDom = true; 47 66 return this; … … 49 68 50 69 51 public ClarinFCSClientBuilder unkownDataViewAsString() { 70 /** 71 * Parse unknown data views into a String. 72 * 73 * @return this {@link ClarinFCSClientBuilder} instance 74 * @see DataViewParserGenericString 75 */ 76 public ClarinFCSClientBuilder unknownDataViewAsString() { 52 77 unknownAsDom = false; 53 78 return this; … … 55 80 56 81 82 /** 83 * Set default SRU version to be used. 84 * 85 * @param defaultVersion 86 * the default SRU version to be used 87 * 88 * @return this {@link ClarinFCSClientBuilder} instance 89 */ 90 public ClarinFCSClientBuilder setDefaultSRUVersion( 91 final SRUVersion defaultVersion) { 92 if (defaultVersion == null) { 93 throw new NullPointerException("defaultVersion == null"); 94 } 95 this.defaultVersion = defaultVersion; 96 return this; 97 } 98 99 100 /** 101 * Enable support for legacy CLARIN-FCS endpoints. 102 * 103 * @return this {@link ClarinFCSClientBuilder} instance 104 */ 105 public ClarinFCSClientBuilder enableLegacySupport() { 106 legacySupport = true; 107 return this; 108 } 109 110 111 /** 112 * Disable support for legacy CLARIN-FCS endpoints. 113 * 114 * @return this {@link ClarinFCSClientBuilder} instance 115 */ 116 public ClarinFCSClientBuilder disableLegacySupport() { 117 legacySupport = false; 118 return this; 119 } 120 121 122 /** 123 * Get the timeout in milliseconds until a connection is established. 124 * 125 * @return this connect timeout in milliseconds 126 */ 127 public int getConnectTimeout() { 128 return connectTimeout; 129 } 130 131 132 /** 133 * Set the timeout in milliseconds until a connection is established. 134 * <p> 135 * A timeout value of <code>0</code> is interpreted as an infinite timeout; 136 * <code>-1</code> is interpreted as system default. 137 * </p> 138 * 139 * @param connectTimeout 140 * the connect timeout in milliseconds 141 * @return this {@link ClarinFCSClientBuilder} instance 142 */ 143 public ClarinFCSClientBuilder setConnectTimeout(int connectTimeout) { 144 if (connectTimeout < -1) { 145 throw new IllegalArgumentException("connectTimeout < -1"); 146 } 147 this.connectTimeout = connectTimeout; 148 return this; 149 } 150 151 152 /** 153 * Get the socket timeout (<code>SO_TIMEOUT</code>) in milliseconds, 154 * which is the timeout for waiting for data. 155 * 156 * @return socketTimeout 157 * the socket timeout in milliseconds 158 */ 159 public int getSocketTimeout() { 160 return socketTimeout; 161 } 162 163 164 /** 165 * Set the socket timeout (<code>SO_TIMEOUT</code>) in milliseconds, which 166 * is the timeout for waiting for data. 167 * <p> 168 * A timeout value of <code>0</code> is interpreted as an infinite timeout; 169 * <code>-1</code> is interpreted as system default. 170 * </p> 171 * 172 * @param socketTimeout 173 * the socket timeout in milliseconds 174 * @return this {@link ClarinFCSClientBuilder} instance 175 */ 176 public ClarinFCSClientBuilder setSocketTimeout(int socketTimeout) { 177 if (socketTimeout < -1) { 178 throw new IllegalArgumentException("socketTimeout < -1"); 179 } 180 this.socketTimeout = socketTimeout; 181 return this; 182 } 183 184 185 /** 186 * Register a data view parser. 187 * 188 * @param parser 189 * the data view parser to be registered 190 * @return this {@link ClarinFCSClientBuilder} instance 191 * @throws IllegalArgumentException 192 * if an error occurred while registering the data view parser 193 */ 57 194 public ClarinFCSClientBuilder registerDataViewParser(DataViewParser parser) { 58 195 if (parser == null) { … … 74 211 75 212 76 public ClarinFCSClientBuilder enableLegacySupport() { 77 legacySupport = true; 78 return this; 79 } 80 81 82 public ClarinFCSClientBuilder disableLegacySupport() { 83 legacySupport = false; 84 return this; 213 /** 214 * Create a {@link SRUSimpleClient} instance. 215 * 216 * @return a configured {@link SRUSimpleClient} instance 217 */ 218 public SRUSimpleClient buildSimpleClient() { 219 return new SRUSimpleClient(makeClientConfig()); 220 } 221 222 223 /** 224 * Create a {@link SRUClient} instance. 225 * 226 * @return a configured {@link SRUClient} instance 227 */ 228 public SRUClient buildClient() { 229 return new SRUClient(makeClientConfig()); 230 } 231 232 233 /** 234 * Create a {@link SRUThreadedClient} instance. 235 * 236 * @return a configured {@link SRUThreadedClient} instance 237 */ 238 public SRUThreadedClient buildThreadedClient() { 239 return new SRUThreadedClient(makeClientConfig()); 85 240 } 86 241 87 242 88 243 @SuppressWarnings("deprecation") 89 public SRUSimpleClient buildSimpleClient() { 244 private SRUClientConfig makeClientConfig() { 245 final SRUClientConfig.Builder builder = new SRUClientConfig.Builder(); 246 builder 247 .setDefaultVersion(defaultVersion) 248 .setConnectTimeout(connectTimeout) 249 .setSocketTimeout(socketTimeout); 90 250 final List<DataViewParser> p = finalizeDataViewParsers(); 91 92 SRUSimpleClient client = new SRUSimpleClient(defaultSruVersion); 93 client.registerRecordParser(new ClarinFCSRecordDataParser(p)); 251 builder.addRecordDataParser(new ClarinFCSRecordDataParser(p)); 94 252 if (legacySupport) { 95 client.registerRecordParser(new LegacyClarinFCSRecordDataParser(p)); 96 } 97 return client; 98 } 99 100 101 @SuppressWarnings("deprecation") 102 public SRUClient buildClient() { 103 final List<DataViewParser> p = finalizeDataViewParsers(); 104 105 SRUClient client = new SRUClient(defaultSruVersion); 106 client.registerRecordParser(new ClarinFCSRecordDataParser(p)); 107 if (legacySupport) { 108 client.registerRecordParser(new LegacyClarinFCSRecordDataParser(p)); 109 } 110 return client; 111 } 112 113 114 @SuppressWarnings("deprecation") 115 public SRUThreadedClient buildThreadedClient() { 116 final List<DataViewParser> p = finalizeDataViewParsers(); 117 118 SRUThreadedClient client = new SRUThreadedClient(defaultSruVersion); 119 client.registerRecordParser(new ClarinFCSRecordDataParser(p)); 120 if (legacySupport) { 121 client.registerRecordParser(new LegacyClarinFCSRecordDataParser(p)); 122 } 123 return client; 253 builder.addRecordDataParser(new LegacyClarinFCSRecordDataParser(p)); 254 } 255 return builder.build(); 124 256 } 125 257 … … 143 275 144 276 145 public static ClarinFCSClientBuilder create() {146 return new ClarinFCSClientBuilder();147 }148 149 150 public static ClarinFCSClientBuilder create(boolean unknownAsDom) {151 return new ClarinFCSClientBuilder(unknownAsDom);152 }153 154 155 277 private static boolean doRegisterDataViewParser( 156 278 List<DataViewParser> parsers, DataViewParser parser) { -
SRUClient/trunk/src/test/java/eu/clarin/sru/client/TestClient.java
r5743 r5748 32 32 logger.info("initializing client ..."); 33 33 34 SRUClient client = ClarinFCSClientBuilder.create()35 . defaults()36 .unk ownDataViewAsString()34 SRUClient client = new ClarinFCSClientBuilder() 35 .addDefaultDataViewParsers() 36 .unknownDataViewAsString() 37 37 .enableLegacySupport() 38 38 .buildClient(); -
SRUClient/trunk/src/test/java/eu/clarin/sru/client/TestSimpleClient.java
r5743 r5748 34 34 logger.info("initializing client ..."); 35 35 36 SRUSimpleClient client = ClarinFCSClientBuilder 37 .create() 38 .defaults() 39 .unkownDataViewAsString() 36 SRUSimpleClient client = new ClarinFCSClientBuilder() 37 .addDefaultDataViewParsers() 38 .unknownDataViewAsString() 40 39 .buildSimpleClient(); 41 40 -
SRUClient/trunk/src/test/java/eu/clarin/sru/client/TestThreadedClient.java
r5743 r5748 34 34 logger.info("initializing client ..."); 35 35 36 SRUThreadedClient client = ClarinFCSClientBuilder 37 .create() 38 .defaults() 36 SRUThreadedClient client = new ClarinFCSClientBuilder() 37 .addDefaultDataViewParsers() 39 38 .buildThreadedClient(); 40 39 -
SRUClient/trunk/src/test/java/eu/clarin/sru/client/TestThreadedClientCallback.java
r5743 r5748 33 33 logger.info("initializing client ..."); 34 34 35 SRUThreadedClient client = ClarinFCSClientBuilder 36 .create() 37 .defaults() 35 SRUThreadedClient client = new ClarinFCSClientBuilder() 36 .addDefaultDataViewParsers() 38 37 .buildThreadedClient(); 39 38
Note: See TracChangeset
for help on using the changeset viewer.