Changeset 1981


Ignore:
Timestamp:
06/08/12 13:22:02 (12 years ago)
Author:
oschonef
Message:
  • wrap every operation in own try..catch
File:
1 edited

Legend:

Unmodified
Added
Removed
  • SRUClient/trunk/src/main/java/eu/clarin/sru/client/TestClient.java

    r1973 r1981  
    1919    public static void main(String[] args) {
    2020        if (args.length > 0) {
     21            logger.info("initializing client ...");
     22            SRUClient client = new SRUClient(SRUVersion.VERSION_1_2);
    2123            try {
    22                 logger.info("initializing client ...");
    23                 SRUClient client = new SRUClient(SRUVersion.VERSION_1_2);
    2424                client.registerRecordParser(new ClarinFederatedContentSearchRecordParser());
     25            } catch (SRUClientException e) {
     26                logger.error("error adding record parser", e);
     27                System.exit(1);
     28            }
    2529
    26                 SRUDefaultHandlerAdapter handler = new SRUDefaultHandlerAdapter() {
    27                     @Override
    28                     public void onFatalError(List<SRUDiagnostic> diagnostics)
    29                             throws SRUClientException {
    30                         for (SRUDiagnostic diagnostic : diagnostics) {
    31                             logger.info(
    32                                     "onFatalError: uri={}, detail={}, message={}",
    33                                     new Object[] { diagnostic.getURI(),
    34                                             diagnostic.getDetails(),
    35                                             diagnostic.getMessage() });
    36                         }
     30            SRUDefaultHandlerAdapter handler = new SRUDefaultHandlerAdapter() {
     31                @Override
     32                public void onFatalError(List<SRUDiagnostic> diagnostics)
     33                        throws SRUClientException {
     34                    for (SRUDiagnostic diagnostic : diagnostics) {
     35                        logger.info("onFatalError: uri={}, detail={}, message={}",
     36                                new Object[] { diagnostic.getURI(),
     37                                        diagnostic.getDetails(),
     38                                        diagnostic.getMessage() });
    3739                    }
     40                }
    3841
    39                     @Override
    40                     public void onRequestStatistics(int bytes,
    41                             long millisTotal, long millisNetwork,
    42                             long millisParsing) {
    43                         logger.info(
    44                                 "onRequestStatistics(): {} bytes in {} millis",
    45                                 bytes, millisTotal);
     42                @Override
     43                public void onRequestStatistics(int bytes, long millisTotal,
     44                        long millisNetwork, long millisParsing) {
     45                    logger.info("onRequestStatistics(): {} bytes in {} millis",
     46                            bytes, millisTotal);
     47                }
     48
     49                @Override
     50                public void onStartTerms() throws SRUClientException {
     51                    logger.info("onStartTerms()");
     52                }
     53
     54                @Override
     55                public void onFinishTerms() throws SRUClientException {
     56                    logger.info("onFinishTerms()");
     57                }
     58
     59                @Override
     60                public void onTerm(String value, int numberOfRecords,
     61                        String displayTerm, WhereInList whereInList)
     62                        throws SRUClientException {
     63                    logger.info("onTerm(): value = {}, numberOfRecords = {}, displayTerm = {}, whereInList = {}",
     64                            new Object[] { value, numberOfRecords, displayTerm,
     65                                    whereInList });
     66                }
     67
     68                @Override
     69                public void onStartRecords(int numberOfRecords,
     70                        int resultSetId, int resultSetIdleTime)
     71                        throws SRUClientException {
     72                    logger.info("onStartRecords(): numberOfRecords = {}",
     73                            numberOfRecords);
     74                }
     75
     76                @Override
     77                public void onFinishRecords(int nextRecordPosition)
     78                        throws SRUClientException {
     79                    logger.info("onFinishRecords(): nextRecordPosition = {}",
     80                            nextRecordPosition);
     81                }
     82
     83                @Override
     84                public void onRecord(String schema, String identifier,
     85                        int position, XMLStreamReader reader)
     86                        throws XMLStreamException, SRUClientException {
     87                    logger.info("onRecord(): schema = {}, identifier = {}, position = {}",
     88                            new Object[] { schema, identifier, position });
     89                    /* just try to read ... */
     90                    while (reader.hasNext()) {
     91                        reader.next();
    4692                    }
     93                }
    4794
    48                     @Override
    49                     public void onStartTerms() throws SRUClientException {
    50                         logger.info("onStartTerms()");
     95                @Override
     96                public void onRecord(String identifier, int position,
     97                        SRURecordData data) throws SRUClientException {
     98                    logger.info("onRecord(): identifier = {}, position = {}, schema = {}",
     99                            new Object[] { identifier, position,
     100                                    data.getRecordSchema() });
     101                    if (ClarinFederatedContentSearchRecordParser.FCS_NS
     102                            .equals(data.getRecordSchema())) {
     103                        ClarinFederatedContentSearchRecordData record = (ClarinFederatedContentSearchRecordData) data;
     104                        logger.info("CLARIN-FCS: \"{}\"/\"{}\"/\"{}\"",
     105                                new Object[] { record.getLeft(),
     106                                        record.getKeyword(), record.getRight() });
    51107                    }
     108                }
    52109
    53                     @Override
    54                     public void onFinishTerms() throws SRUClientException {
    55                         logger.info("onFinishTerms()");
    56                     }
     110                @Override
     111                public void onSurrogateRecord(String identifier, int position,
     112                        SRUDiagnostic data) throws SRUClientException {
     113                    logger.info("onSurrogateRecord: identifier = {}, position = {}, uri={}, detail={}, message={}",
     114                            new Object[] { identifier, position, data.getURI(),
     115                                    data.getDetails(), data.getMessage() });
     116                }
     117            };
    57118
    58                     @Override
    59                     public void onTerm(String value, int numberOfRecords,
    60                             String displayTerm, WhereInList whereInList)
    61                             throws SRUClientException {
    62                         logger.info("onTerm(): value = {}, numberOfRecords = {}, displayTerm = {}, whereInList = {}",
    63                                 new Object[] { value, numberOfRecords,
    64                                         displayTerm, whereInList });
    65                     }
     119            try {
     120                logger.info("performing 'explain' request ...");
     121                SRUExplainRequest request = new SRUExplainRequest(args[0]);
     122                client.explain(request, handler);
     123            } catch (SRUClientException e) {
     124                logger.error("a fatal error occured while performing 'explain' request", e);
     125            }
    66126
    67                     @Override
    68                     public void onStartRecords(int numberOfRecords,
    69                             int resultSetId, int resultSetIdleTime)
    70                             throws SRUClientException {
    71                         logger.info("onStartRecords(): numberOfRecords = {}",
    72                                 numberOfRecords);
    73                     }
     127            try {
     128                logger.info("performing 'scan' request ...");
     129                SRUScanRequest request = new SRUScanRequest(args[0]);
     130                request.setScanClause("cmd.collections");
     131                request.setMaximumTerms(2);
     132                client.scan(request, handler);
     133            } catch (SRUClientException e) {
     134                logger.error("a fatal error occured while performing 'scan' request", e);
     135            }
    74136
    75                     @Override
    76                     public void onFinishRecords(int nextRecordPosition)
    77                             throws SRUClientException {
    78                         logger.info("onFinishRecords(): nextRecordPosition = {}",
    79                                 nextRecordPosition);
    80                     }
    81 
    82                     @Override
    83                     public void onRecord(String schema, String identifier,
    84                             int position, XMLStreamReader reader)
    85                             throws XMLStreamException, SRUClientException {
    86                         logger.info("onRecord(): schema = {}, identifier = {}, position = {}",
    87                                 new Object[] { schema, identifier, position });
    88                         /* just try to read ... */
    89                         while (reader.hasNext()) {
    90                             reader.next();
    91                         }
    92                     }
    93 
    94                     @Override
    95                     public void onRecord(String identifier, int position,
    96                             SRURecordData data) throws SRUClientException {
    97                         logger.info("onRecord(): identifier = {}, position = {}, schema = {}",
    98                                 new Object[] { identifier, position,
    99                                         data.getRecordSchema() });
    100                         if (ClarinFederatedContentSearchRecordParser.FCS_NS
    101                                 .equals(data.getRecordSchema())) {
    102                             ClarinFederatedContentSearchRecordData record = (ClarinFederatedContentSearchRecordData) data;
    103                             logger.info("CLARIN-FCS: \"{}\"/\"{}\"/\"{}\"", new Object[] { record.getLeft(), record.getKeyword(), record.getRight() });
    104                         }
    105                     }
    106 
    107                     @Override
    108                     public void onSurrogateRecord(String identifier,
    109                             int position, SRUDiagnostic data)
    110                             throws SRUClientException {
    111                         logger.info("onSurrogateRecord: identifier = {}, position = {}, uri={}, detail={}, message={}",
    112                                 new Object[] { identifier, position,
    113                                         data.getURI(), data.getDetails(),
    114                                         data.getMessage() });
    115                     }
    116                 };
    117 
    118                 logger.info("performing 'explain' request ...");
    119                 SRUExplainRequest r1 = new SRUExplainRequest(args[0]);
    120                 client.explain(r1, handler);
    121 
    122                 logger.info("performing 'scan' request ...");
    123                 SRUScanRequest r2 = new SRUScanRequest(args[0]);
    124                 r2.setScanClause("cmd.collections");
    125                 r2.setMaximumTerms(2);
    126                 client.scan(r2, handler);
    127 
    128                 logger.info("performing 'scan' request ...");
    129                 SRUSearchRetrieveRequest r3 =
     137            try {
     138                logger.info("performing 'searchRetrieve' request ...");
     139                SRUSearchRetrieveRequest request =
    130140                        new SRUSearchRetrieveRequest(args[0]);
    131                 r3.setQuery("Faustus");
    132                 r3.setRecordSchema(ClarinFederatedContentSearchRecordParser.FCS_RECORD_SCHEMA);
    133                 r3.setMaximumRecords(5);
    134                 r3.setExtraRequestData("x-indent-response", "4");
    135                 client.searchRetrieve(r3, handler);
     141                request.setQuery("Faustus");
     142                request.setRecordSchema(ClarinFederatedContentSearchRecordParser.FCS_RECORD_SCHEMA);
     143                request.setMaximumRecords(5);
     144                request.setExtraRequestData("x-indent-response", "4");
     145                client.searchRetrieve(request, handler);
    136146            } catch (SRUClientException e) {
    137                 logger.error("some error occured", e);
     147                logger.error("a fatal error occured while performing 'searchRetrieve' request", e);
    138148            }
    139149        } else {
Note: See TracChangeset for help on using the changeset viewer.