Changeset 289 for tools

Show
Ignore:
Timestamp:
04/07/09 17:13:05 (11 months ago)
Author:
anton
Message:

Added proper resource handler instantiation and connection pools

Location:
tools/routingservice/branches/wrs-2.0
Files:
4 added
5 modified

Legend:

Unmodified
Added
Removed
  • tools/routingservice/branches/wrs-2.0/data/resources.xml

    r286 r289  
    44xsi:noNamespaceSchemaLocation="http://wrs.postlbs.org/xsd/1.1.0/resource.xsd" 
    55> 
    6         <resource name="pgrouting" title="pgRouting instance on Dumbo server"> 
     6        <resource type="database" name="pgrouting" title="pgRouting instance on Dumbo server"> 
    77                <description>My test profile I use for testing</description> 
    88                <parameters> 
  • tools/routingservice/branches/wrs-2.0/src/WRS.java

    r287 r289  
     1import handler.HandlerFactory; 
     2import handler.ResourceHandler; 
     3 
    14import java.io.IOException; 
    25import java.util.ArrayList; 
     
    101104                                ServiceRequest serviceRequest = null; 
    102105                                String result = ""; 
    103                                  
     106 
    104107                                try 
    105108                                { 
     
    117120                                        else 
    118121                                        { 
    119                                                 result = resource.processRequest(serviceRequest); 
    120                                         } 
    121                                          
     122                                                try 
     123                                                { 
     124                                                        ResourceHandler handler = HandlerFactory.Handler 
     125                                                                        .valueOf(resource.getName().toUpperCase()) 
     126                                                                        .getInstance(); 
     127                                                        handler.setResource(resource); 
     128 
     129                                                        Vector<Parameter> output = handler 
     130                                                                        .handle(serviceRequest); 
     131                                                } 
     132                                                // TODO process the error more carefully 
     133                                                catch (InstantiationException e) 
     134                                                { 
     135                                                        log.logger.warning("Wrong resource " 
     136                                                                        + resource.getName()); 
     137                                                } 
     138                                                catch (IllegalAccessException e) 
     139                                                { 
     140                                                        log.logger.warning("Wrong resource " 
     141                                                                        + resource.getName()); 
     142                                                } 
     143                                        } 
     144 
    122145                                } 
    123146                                catch (InvalidVersionException e) 
     
    335358                        { 
    336359                                util.resource.Resources.Resource res = li.next(); 
    337                                 Resource resource = new Resource(res.getName(), res.getType()); 
     360                                Resource resource = new Resource(res.getName(), res.getType(), 
     361                                                res.getUrl(), res.getUser(), res.getPassword()); 
    338362 
    339363                                ListIterator<Parameter> pi = res.getParameters().getParameter() 
  • tools/routingservice/branches/wrs-2.0/src/handler/PgRoutingHandler.java

    r287 r289  
    11package handler; 
    22 
     3import java.sql.Connection; 
    34import java.util.Hashtable; 
    45import java.util.Vector; 
    56 
    67import model.Resource; 
     8import util.ObjectPool; 
    79import util.Parameter; 
    810import util.ServiceRequest; 
     11import util.db.JDBCConnectionPool; 
    912 
    1013import org.antlr.stringtemplate.StringTemplate; 
     
    1417        private Resource resource; 
    1518        private Hashtable<String, String> functions; 
    16          
     19 
    1720        public static final String SRID = "srid"; 
    1821        public static final String UNITS = "units"; 
    19          
     22 
    2023        public PgRoutingHandler() 
    2124        { 
     
    3033                Vector<Parameter> out = new Vector<Parameter>(); 
    3134 
    32                 StringTemplate queryTemplate = new StringTemplate(this.resource.getQuery()); 
     35                if (this.resource != null) 
     36                { 
    3337 
     38                        StringTemplate queryTemplate = new StringTemplate(this.resource 
     39                                        .getQuery()); 
     40                        ObjectPool<Connection> pool = this.resource.getPool(); 
     41                        Connection con = pool.checkOut(); 
     42                         
     43                        //TODO send query and get a result 
     44                         
     45                        pool.checkIn(con); 
     46                } 
    3447                return out; 
    3548        } 
  • tools/routingservice/branches/wrs-2.0/src/handler/ResourceHandler.java

    r287 r289  
    99public interface ResourceHandler 
    1010{ 
     11 
    1112        public void setResource(Resource resource); 
    1213        public Vector<Parameter> handle(ServiceRequest request); 
     14 
    1315} 
  • tools/routingservice/branches/wrs-2.0/src/model/Resource.java

    r287 r289  
    11package model; 
    22 
     3import handler.HandlerFactory; 
     4import handler.PgRoutingHandler; 
    35import handler.ResourceHandler; 
    46 
     7import java.lang.reflect.InvocationTargetException; 
    58import java.util.Hashtable; 
    69 
     10import org.restlet.Handler; 
     11 
     12import util.ObjectPool; 
    713import util.Parameter; 
    814import util.ServiceRequest; 
     15import util.db.JDBCConnectionPool; 
    916 
    1017public class Resource 
     
    2229        private Hashtable<String, Parameter> parameters; 
    2330        private Hashtable<String, Service> services; 
    24          
    25         private ResourceHandler handler; 
    2631 
    27         public Resource(String name, String type) 
     32        private enum PoolTypes 
     33        { 
     34                DATABASE(JDBCConnectionPool.class); 
     35 
     36                private Class<? extends ObjectPool> poolType; 
     37 
     38                PoolTypes(Class<? extends ObjectPool> p) 
     39                { 
     40                        this.poolType = p; 
     41                } 
     42 
     43                public ObjectPool getInstance(String url, String user, String password) 
     44                                throws InstantiationException, IllegalAccessException, 
     45                                IllegalArgumentException, SecurityException, 
     46                                InvocationTargetException 
     47                { 
     48                        System.out.println(">>>>" + poolType.getConstructors()[0].toString()); 
     49                         
     50                        return (ObjectPool) poolType.getConstructors()[0] 
     51                                        .newInstance(new Object[] { url, user, password }); 
     52                } 
     53        } 
     54 
     55        private ObjectPool pool; 
     56 
     57        public Resource(String name, String type, String url, String user, String password) 
    2858        { 
    2959                super(); 
    3060                this.name = name; 
    3161                this.type = type; 
    32                  
     62 
     63                try 
     64                { 
     65                        this.pool = PoolTypes.valueOf(type.toUpperCase()).getInstance(url, user, password); 
     66                } 
     67                // TODO process the error more carefully 
     68                catch (InstantiationException e) 
     69                { 
     70                        System.out.println("Wrong resource type " + this.type); 
     71                        e.printStackTrace(); 
     72                } 
     73                catch (IllegalAccessException e) 
     74                { 
     75                        System.out.println("Wrong resource type " + this.type); 
     76                        e.printStackTrace(); 
     77                } 
     78                catch (IllegalArgumentException e) 
     79                { 
     80                        // TODO Auto-generated catch block 
     81                        e.printStackTrace(); 
     82                } 
     83                catch (SecurityException e) 
     84                { 
     85                        // TODO Auto-generated catch block 
     86                        e.printStackTrace(); 
     87                } 
     88                catch (InvocationTargetException e) 
     89                { 
     90                        // TODO Auto-generated catch block 
     91                        e.printStackTrace(); 
     92                } 
     93 
    3394                parameters = new Hashtable<String, Parameter>(); 
    3495                services = new Hashtable<String, Service>(); 
    3596        } 
    36          
     97 
    3798        public String getName() 
    3899        { 
    39100                return name; 
    40         }        
     101        } 
    41102 
    42103        public String getDescription() 
     
    120181        } 
    121182 
    122         public String processRequest(ServiceRequest serviceRequest) 
     183        public String getType() 
    123184        { 
    124                 // TODO Auto-generated method stub 
    125                 return null; 
     185                return type; 
     186        } 
     187 
     188        public ObjectPool getPool() 
     189        { 
     190                return pool; 
    126191        } 
    127192