Changeset 7315


Ignore:
Timestamp:
11/08/10 17:05:51 (9 years ago)
Author:
mnagni
Message:

included the Apache common-io and some utilities

Location:
RevitalizationWS/common/trunk
Files:
1 added
3 edited

Legend:

Unmodified
Added
Removed
  • RevitalizationWS/common/trunk/pom.xml

    r7244 r7315  
    4242        <dependencies> 
    4343                <dependency> 
     44                        <groupId>commons-io</groupId> 
     45                        <artifactId>commons-io</artifactId> 
     46                        <version>1.3.2</version> 
     47                </dependency> 
     48         
     49                <dependency> 
    4450                        <groupId>org.slf4j</groupId> 
    4551                        <artifactId>log4j-over-slf4j</artifactId> 
  • RevitalizationWS/common/trunk/src/main/java/ndg/common/MessageFormatter.java

    r7283 r7315  
    115115         * <code>null</code> or formatting error occur. 
    116116         **/             
    117         public String getFormattedText(String text, Object[] params) throws NdgExecutionException {              
     117        public static String getFormattedText(String text, Object[] params) throws NdgExecutionException {               
    118118                MessageFormat formatter = new MessageFormat(""); 
    119119                 
  • RevitalizationWS/common/trunk/src/main/java/ndg/common/StreamConnector.java

    r7313 r7315  
    66import java.io.OutputStream; 
    77 
     8import ndg.common.exception.NdgExecutionException; 
     9 
    810/** 
    9  * Reads an <code>InputStream</code> and writes its content in  
    10  * an <code>OutputStream</code>. The actual implementation reads the given  
    11  * <code>InputStream</code> and store its content in an internal buffer. 
    12  * Such buffer is written in the given <code>OutputStream</code> if the bytes  
    13  * read are less than the buffer capacity otherwise the data are store internally for 
    14  * a future writing operation. 
    15  * To allow more then one instance to be bind to the same <code>OutputStream</code>, without  
    16  * interlace one write operation with another, the constructor {@link StreamConnector#StreamConnector(InputStream, String, OutputStream, Object, int)} 
    17  * allows to pass a <code>synch</code> used to prevent such interlace. 
    18  * Based on the <code>Thread</code> class, starts its activity once the <code>start()</code> 
    19  * is called and stops its activity (exhausting all the remaining <code>InputStream</code>) when the 
     11 * Reads an <code>InputStream</code> and writes its content in an 
     12 * <code>OutputStream</code>. The actual implementation reads the given 
     13 * <code>InputStream</code> and store its content in an internal buffer. Such 
     14 * buffer is written in the given <code>OutputStream</code> if the bytes read 
     15 * are less than the buffer capacity otherwise the data are store internally for 
     16 * a future writing operation. To allow more then one instance to be bind to the 
     17 * same <code>OutputStream</code>, without interlace one write operation with 
     18 * another, the constructor 
     19 * {@link StreamConnector#StreamConnector(InputStream, String, OutputStream, Object, int)} 
     20 * allows to pass a <code>synch</code> used to prevent such interlace. Based on 
     21 * the <code>Thread</code> class, starts its activity once the 
     22 * <code>start()</code> is called and stops its activity (exhausting all the 
     23 * remaining <code>InputStream</code>) when the 
    2024 * {@link StreamConnector#terminateConnector()} is called. 
    2125 **/ 
     
    2428        private final OutputStream os; 
    2529        private final String id; 
    26         private final Object sync;  
     30        private final Object sync; 
    2731        private final byte[] buf; 
    2832        private boolean stop = false; 
     
    3034        /** 
    3135         * Returns an instance. Uses a default bufSize=1024. 
    32          * @param is the InputStream from which read the data stream 
    33          * @param id an identifier string appended before the internal buffer is written into the OutputStream 
    34          * @param os the OutputStream where write the input stream data 
     36         *  
     37         * @param is 
     38         *            the InputStream from which read the data stream 
     39         * @param id 
     40         *            an identifier string appended before the internal buffer is 
     41         *            written into the OutputStream 
     42         * @param os 
     43         *            the OutputStream where write the input stream data 
     44         * @throws NdgExecutionException 
    3545         **/ 
    36         public StreamConnector(InputStream is, String id, OutputStream os) { 
     46        public StreamConnector(InputStream is, String id, OutputStream os) throws NdgExecutionException { 
    3747                this(is, id, os, 1024); 
    3848        } 
    39          
     49 
    4050        /** 
    4151         * Returns an instance. 
    42          * @param is the InputStream from which read the data stream 
    43          * @param id an identifier string appended before the internal buffer is written into the OutputStream 
    44          * @param os the OutputStream where write the input stream data 
    45          * @param bufSize the size of the buffer used to read the input stream 
     52         *  
     53         * @param is 
     54         *            the InputStream from which read the data stream 
     55         * @param id 
     56         *            an identifier string appended before the internal buffer is 
     57         *            written into the OutputStream 
     58         * @param os 
     59         *            the OutputStream where write the input stream data 
     60         * @param bufSize 
     61         *            the size of the buffer used to read the input stream 
     62         * @throws NdgExecutionException 
    4663         **/ 
    47         public StreamConnector(InputStream is, String id, OutputStream os, int bufSize) { 
     64        public StreamConnector(InputStream is, String id, OutputStream os, int bufSize) throws NdgExecutionException { 
    4865                this(is, id, os, new Object(), bufSize); 
    4966        } 
     
    5168        /** 
    5269         * Returns an instance. 
    53          * @param is the InputStream from which read the data stream 
    54          * @param id an identifier string appended before the internal buffer is written into the OutputStream 
    55          * @param os the OutputStream where write the input stream data 
    56          * @param synch the object used to synchronize the writing operation 
    57          * @param bufSize the size of the buffer used to read the input stream 
     70         *  
     71         * @param is 
     72         *            the InputStream from which read the data stream 
     73         * @param id 
     74         *            an identifier string appended before the internal buffer is 
     75         *            written into the OutputStream 
     76         * @param os 
     77         *            the OutputStream where write the input stream data 
     78         * @param synch 
     79         *            the object used to synchronize the writing operation 
     80         * @throws NdgExecutionException 
    5881         **/ 
    59         public StreamConnector(InputStream is, String id, OutputStream os, Object sync, int bufSize) { 
     82        public StreamConnector(InputStream is, String id, OutputStream os, Object sync) throws NdgExecutionException { 
     83                this(is, id, os, sync, 1024); 
     84        } 
     85 
     86        /** 
     87         * Returns an instance. 
     88         *  
     89         * @param is 
     90         *            the InputStream from which read the data stream 
     91         * @param id 
     92         *            an identifier string appended before the internal buffer is 
     93         *            written into the OutputStream 
     94         * @param os 
     95         *            the OutputStream where write the input stream data 
     96         * @param synch 
     97         *            the object used to synchronize the writing operation 
     98         * @param bufSize 
     99         *            the size of the buffer used to read the input stream 
     100         * @throws NdgExecutionException 
     101         *             if any of the parameter is not valid 
     102         **/ 
     103        public StreamConnector(InputStream is, String id, OutputStream os, Object sync, int bufSize) 
     104                        throws NdgExecutionException { 
    60105                super(); 
     106                if (is == null) 
     107                        throw new NdgExecutionException("InputStream cannot be null"); 
     108                if (os == null) 
     109                        throw new NdgExecutionException("OutputStream cannot be null"); 
     110                if (sync == null) 
     111                        throw new NdgExecutionException("The syncronization object cannot be null"); 
     112                if (bufSize < 1) 
     113                        throw new NdgExecutionException("The bufSize cannot be 0 or less"); 
    61114                this.is = is; 
    62115                this.os = os; 
     
    65118                buf = new byte[bufSize]; 
    66119        } 
    67          
     120 
    68121        @Override 
    69122        public void run() { 
    70                 try { 
    71                         int size = 0; 
    72                         ByteArrayOutputStream sb = new ByteArrayOutputStream(); 
    73                         boolean terminate = false; 
    74                         while (true) { 
     123                int size = 0; 
     124                ByteArrayOutputStream sb = new ByteArrayOutputStream(); 
     125                boolean terminate = false; 
     126                while (true) { 
     127 
     128                        try { 
    75129                                size = is.read(buf); 
    76                                 if (size<buf.length-1) { 
     130                        } catch (IOException e1) { 
     131                                continue; 
     132                        } 
     133                        if (size < buf.length - 1) { 
     134                                try { 
    77135                                        sb.write(buf); 
    78136                                        sb.flush(); 
    79137                                        if (sb.size() > 0) { 
    80138                                                writeToOutput(sb.toByteArray()); 
    81                                         }  
    82                                         if (terminate) 
    83                                                 break; 
    84                                         if ((size==-1 && stop)) 
    85                                                 terminate = true; 
     139                                        } 
     140                                } catch (IOException e) { 
     141                                        // throw new RuntimeException(e); 
    86142                                } 
    87                         }                                
    88                 } catch (IOException e) { 
    89                         // TODO Auto-generated catch block 
    90                         e.printStackTrace(); 
    91                 }                        
     143                        } 
     144                        if (terminate) 
     145                                break; 
     146                        if ((size == -1 && stop)) 
     147                                terminate = true; 
     148                } 
     149 
    92150        } 
    93          
    94         private void writeToOutput(byte[] internalBuf) { 
     151 
     152        private void writeToOutput(byte[] internalBuf) throws IOException { 
    95153                synchronized (sync) { 
    96                         try { 
    97                                 os.write((id + "\n").getBytes()); 
    98                                 os.write(internalBuf); 
    99                         } catch (IOException e) { 
    100                                 // TODO Auto-generated catch block 
    101                                 e.printStackTrace(); 
    102                         } 
     154                        os.write((id + "\n").getBytes()); 
     155                        os.write(internalBuf); 
    103156                } 
    104157        } 
    105          
     158 
    106159        /** 
    107          * Signals to the underlying thread that is has to finish. 
    108          * Because this methods just set a flag, in order to allow the thread to  
    109          * read at least the last message from the InputStream, only the  
    110          * Thread.isAlive() method can assure that the thread is really terminated.   
     160         * Signals to the underlying thread that is has to finish. Because this 
     161         * methods just set a flag, in order to allow the thread to read at least 
     162         * the last message from the InputStream, only the Thread.isAlive() method 
     163         * can assure that the thread is really terminated. 
    111164         **/ 
    112         public void terminateConnector(){ 
     165        public void terminateConnector() { 
    113166                stop = true; 
    114167        } 
    115          
     168 
    116169} 
Note: See TracChangeset for help on using the changeset viewer.