Changeset 5748 for SRUClient


Ignore:
Timestamp:
10/25/14 21:35:19 (10 years ago)
Author:
Oliver Schonefeld
Message:
  • more work-in-progress
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  
    1919import java.util.ArrayDeque;
    2020import java.util.Deque;
    21 import java.util.HashMap;
    2221import java.util.LinkedList;
    2322import java.util.List;
    24 import java.util.Map;
    2523import java.util.concurrent.TimeUnit;
    2624
     
    7775
    7876    /**
    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
    8386     */
    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());
    10289    }
    10390
     
    120107     *            Builders
    121108     */
    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");
    130113        }
    131114        if (documentBuilderFactory == null) {
    132115            throw new NullPointerException("documentBuilderFactory == null");
    133116        }
    134         this.client = new SRUSimpleClient(defaultVersion, parsers);
     117        this.client = new SRUSimpleClient(config);
    135118        this.handler = new Handler();
    136119        try {
     
    145128        }
    146129        reset();
    147     }
    148 
    149 
    150     /**
    151      * Register a record data parser.
    152      *
    153      * @param parser
    154      *            a parser instance
    155      * @throws NullPointerException
    156      *             if any required argument is <code>null</code>
    157      * @throws IllegalArgumentException
    158      *             if the supplied parser is invalid or a parser handing the
    159      *             same record schema is already registered
    160      */
    161     public void registerRecordParser(SRURecordDataParser parser) {
    162         client.registerRecordParser(parser);
    163130    }
    164131
  • SRUClient/trunk/src/main/java/eu/clarin/sru/client/SRUSimpleClient.java

    r5743 r5748  
    6969    private static final String USER_AGENT = "SRU-Client/1.0.0";
    7070    /** default version the client will use, if not otherwise specified */
    71     public static final SRUVersion DEFAULT_SRU_VERSION = SRUVersion.VERSION_1_2;
    7271    private static final String SRU_NS =
    7372            "http://www.loc.gov/zing/srw/";
     
    8988            new SRUExplainRecordDataParser();
    9089
    91     /**
    92      * Constructor. This constructor will create a <em>strict</em> client and
    93      * use the default SRU version.
    94      *
    95      * @see #DEFAULT_SRU_VERSION
    96      */
    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 defaultVersion
    106      *            the default version to use for SRU requests; may be overridden
    107      *            by individual requests
    108      */
    109     public SRUSimpleClient(SRUVersion defaultVersion) {
    110         this(defaultVersion, new HashMap<String, SRURecordDataParser>());
    111     }
    112 
    11390
    11491    /**
    11592     * Constructor.
    11693     *
    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
    132101     */
    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        }
    143125
    144126        // 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());
    182129    }
    183130
  • SRUClient/trunk/src/main/java/eu/clarin/sru/client/SRUThreadedClient.java

    r2942 r5748  
    1818
    1919import java.util.concurrent.Callable;
    20 import java.util.concurrent.ConcurrentHashMap;
    21 import java.util.concurrent.ConcurrentMap;
    2220import java.util.concurrent.ExecutorService;
    2321import java.util.concurrent.Executors;
     
    4341    private static final Logger logger =
    4442            LoggerFactory.getLogger(SRUThreadedClient.class);
    45     private final ConcurrentMap<String, SRURecordDataParser> parsers =
    46             new ConcurrentHashMap<String, SRURecordDataParser>();
    4743    private final DocumentBuilderFactory documentBuilderFactory =
    4844            DocumentBuilderFactory.newInstance();
     
    5248
    5349    /**
    54      * Constructor. This constructor will create a <em>strict</em> client and
    55      * use the default SRU version.
    56      *
    57      * @see SRUSimpleClient#DEFAULT_SRU_VERSION
    58      */
    59     public SRUThreadedClient() {
    60         this(SRUSimpleClient.DEFAULT_SRU_VERSION);
    61     }
    62 
    63 
    64     /**
    6550     * Constructor.
    6651     *
    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) {
    7261        client = new ThreadLocal<SRUClient>() {
    7362            @Override
    7463            protected SRUClient initialValue() {
    7564                logger.debug("instantiated new sru client");
    76                 return new SRUClient(defaultVersion, parsers,
    77                         documentBuilderFactory);
     65                return new SRUClient(config, documentBuilderFactory);
    7866            }
    7967        };
    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());
    11673    }
    11774
  • SRUClient/trunk/src/main/java/eu/clarin/sru/client/fcs/ClarinFCSClientBuilder.java

    r5743 r5748  
    55
    66import eu.clarin.sru.client.SRUClient;
     7import eu.clarin.sru.client.SRUClientConfig;
    78import eu.clarin.sru.client.SRUSimpleClient;
    89import eu.clarin.sru.client.SRUThreadedClient;
     
    1011
    1112
     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 */
    1218public class ClarinFCSClientBuilder {
    1319    private static final boolean DEFAULT_UNKNOWN_AS_DOM =
     
    1622            SRUVersion.VERSION_1_2;
    1723    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;
    2129
    2230
     
    3745
    3846
    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() {
    4053        doRegisterDataViewParser(parsers, new DataViewParserHits());
    4154        return this;
     
    4356
    4457
    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() {
    4665        unknownAsDom = true;
    4766        return this;
     
    4968
    5069
    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() {
    5277        unknownAsDom = false;
    5378        return this;
     
    5580
    5681
     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     */
    57194    public ClarinFCSClientBuilder registerDataViewParser(DataViewParser parser) {
    58195        if (parser == null) {
     
    74211
    75212
    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());
    85240    }
    86241
    87242
    88243    @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);
    90250        final List<DataViewParser> p = finalizeDataViewParsers();
    91 
    92         SRUSimpleClient client = new SRUSimpleClient(defaultSruVersion);
    93         client.registerRecordParser(new ClarinFCSRecordDataParser(p));
     251        builder.addRecordDataParser(new ClarinFCSRecordDataParser(p));
    94252        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();
    124256    }
    125257
     
    143275
    144276
    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 
    155277    private static boolean doRegisterDataViewParser(
    156278            List<DataViewParser> parsers, DataViewParser parser) {
  • SRUClient/trunk/src/test/java/eu/clarin/sru/client/TestClient.java

    r5743 r5748  
    3232            logger.info("initializing client ...");
    3333
    34             SRUClient client = ClarinFCSClientBuilder.create()
    35                     .defaults()
    36                     .unkownDataViewAsString()
     34            SRUClient client = new ClarinFCSClientBuilder()
     35                    .addDefaultDataViewParsers()
     36                    .unknownDataViewAsString()
    3737                    .enableLegacySupport()
    3838                    .buildClient();
  • SRUClient/trunk/src/test/java/eu/clarin/sru/client/TestSimpleClient.java

    r5743 r5748  
    3434            logger.info("initializing client ...");
    3535
    36             SRUSimpleClient client = ClarinFCSClientBuilder
    37                         .create()
    38                         .defaults()
    39                         .unkownDataViewAsString()
     36            SRUSimpleClient client = new ClarinFCSClientBuilder()
     37                        .addDefaultDataViewParsers()
     38                        .unknownDataViewAsString()
    4039                        .buildSimpleClient();
    4140
  • SRUClient/trunk/src/test/java/eu/clarin/sru/client/TestThreadedClient.java

    r5743 r5748  
    3434            logger.info("initializing client ...");
    3535
    36             SRUThreadedClient client = ClarinFCSClientBuilder
    37                     .create()
    38                     .defaults()
     36            SRUThreadedClient client = new ClarinFCSClientBuilder()
     37                    .addDefaultDataViewParsers()
    3938                    .buildThreadedClient();
    4039
  • SRUClient/trunk/src/test/java/eu/clarin/sru/client/TestThreadedClientCallback.java

    r5743 r5748  
    3333            logger.info("initializing client ...");
    3434
    35             SRUThreadedClient client = ClarinFCSClientBuilder
    36                     .create()
    37                     .defaults()
     35            SRUThreadedClient client = new ClarinFCSClientBuilder()
     36                    .addDefaultDataViewParsers()
    3837                    .buildThreadedClient();
    3938
Note: See TracChangeset for help on using the changeset viewer.