| 1 | /* WRS 2.0 |
|---|
| 2 | * Copyright (C) 2009 Anton Patrushev |
|---|
| 3 | * |
|---|
| 4 | * This program is free software; you can redistribute it and/or modify |
|---|
| 5 | * it under the terms of the GNU General Public License as published by |
|---|
| 6 | * the Free Software Foundation; either version 3 of the License, or |
|---|
| 7 | * (at your option) any later version. |
|---|
| 8 | * |
|---|
| 9 | * This program is distributed in the hope that it will be useful, |
|---|
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 12 | * GNU General Public License for more details. |
|---|
| 13 | * |
|---|
| 14 | * You should have received a copy of the GNU General Public License |
|---|
| 15 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
|---|
| 16 | */ |
|---|
| 17 | |
|---|
| 18 | package model; |
|---|
| 19 | |
|---|
| 20 | import java.io.Serializable; |
|---|
| 21 | import java.lang.reflect.InvocationTargetException; |
|---|
| 22 | import java.util.HashMap; |
|---|
| 23 | import java.util.Hashtable; |
|---|
| 24 | |
|---|
| 25 | import util.ObjectPool; |
|---|
| 26 | import util.Parameter; |
|---|
| 27 | import util.db.JDBCConnectionPool; |
|---|
| 28 | import util.web.WebConnectionPool; |
|---|
| 29 | |
|---|
| 30 | public class Resource implements Serializable |
|---|
| 31 | { |
|---|
| 32 | private String name; |
|---|
| 33 | private String description; |
|---|
| 34 | private String title; |
|---|
| 35 | private String type; |
|---|
| 36 | |
|---|
| 37 | private String url; |
|---|
| 38 | private String user; |
|---|
| 39 | private String password; |
|---|
| 40 | private String query; |
|---|
| 41 | |
|---|
| 42 | private HashMap<String, Parameter> parameters; |
|---|
| 43 | private Hashtable<String, Service> services; |
|---|
| 44 | |
|---|
| 45 | private enum PoolTypes |
|---|
| 46 | { |
|---|
| 47 | DATABASE(JDBCConnectionPool.class), WEB(WebConnectionPool.class), SELF(WebConnectionPool.class); |
|---|
| 48 | |
|---|
| 49 | @SuppressWarnings("unchecked") |
|---|
| 50 | private Class< ? extends ObjectPool> poolType; |
|---|
| 51 | |
|---|
| 52 | @SuppressWarnings("unchecked") |
|---|
| 53 | PoolTypes(Class< ? extends ObjectPool> p) |
|---|
| 54 | { |
|---|
| 55 | this.poolType = p; |
|---|
| 56 | } |
|---|
| 57 | |
|---|
| 58 | @SuppressWarnings("unchecked") |
|---|
| 59 | public ObjectPool getInstance(String url, String user, String password) |
|---|
| 60 | throws InstantiationException, IllegalAccessException, |
|---|
| 61 | IllegalArgumentException, SecurityException, |
|---|
| 62 | InvocationTargetException |
|---|
| 63 | { |
|---|
| 64 | return (ObjectPool) poolType.getConstructors()[0] |
|---|
| 65 | .newInstance(new Object[] { url, user, password }); |
|---|
| 66 | } |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | @SuppressWarnings("unchecked") |
|---|
| 70 | private ObjectPool pool; |
|---|
| 71 | |
|---|
| 72 | public Resource(String name, String type, String url, String user, |
|---|
| 73 | String password, String query) |
|---|
| 74 | { |
|---|
| 75 | super(); |
|---|
| 76 | this.name = name; |
|---|
| 77 | this.type = type; |
|---|
| 78 | this.url = url; |
|---|
| 79 | this.user = user; |
|---|
| 80 | this.password = password; |
|---|
| 81 | this.query = query; |
|---|
| 82 | |
|---|
| 83 | try |
|---|
| 84 | { |
|---|
| 85 | this.pool = PoolTypes.valueOf(type.toUpperCase()).getInstance(url, |
|---|
| 86 | user, password); |
|---|
| 87 | } |
|---|
| 88 | // TODO process the error more carefully |
|---|
| 89 | catch (InstantiationException e) |
|---|
| 90 | { |
|---|
| 91 | System.out.println("Wrong resource type " + this.type); |
|---|
| 92 | e.printStackTrace(); |
|---|
| 93 | } |
|---|
| 94 | catch (IllegalAccessException e) |
|---|
| 95 | { |
|---|
| 96 | System.out.println("Wrong resource type " + this.type); |
|---|
| 97 | e.printStackTrace(); |
|---|
| 98 | } |
|---|
| 99 | catch (IllegalArgumentException e) |
|---|
| 100 | { |
|---|
| 101 | // TODO Auto-generated catch block |
|---|
| 102 | e.printStackTrace(); |
|---|
| 103 | } |
|---|
| 104 | catch (SecurityException e) |
|---|
| 105 | { |
|---|
| 106 | // TODO Auto-generated catch block |
|---|
| 107 | e.printStackTrace(); |
|---|
| 108 | } |
|---|
| 109 | catch (InvocationTargetException e) |
|---|
| 110 | { |
|---|
| 111 | // TODO Auto-generated catch block |
|---|
| 112 | e.printStackTrace(); |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| 115 | parameters = new HashMap<String, Parameter>(); |
|---|
| 116 | services = new Hashtable<String, Service>(); |
|---|
| 117 | } |
|---|
| 118 | |
|---|
| 119 | public String getName() |
|---|
| 120 | { |
|---|
| 121 | return name; |
|---|
| 122 | } |
|---|
| 123 | |
|---|
| 124 | public String getDescription() |
|---|
| 125 | { |
|---|
| 126 | return description; |
|---|
| 127 | } |
|---|
| 128 | |
|---|
| 129 | public void setDescription(String description) |
|---|
| 130 | { |
|---|
| 131 | this.description = description; |
|---|
| 132 | } |
|---|
| 133 | |
|---|
| 134 | public String getTitle() |
|---|
| 135 | { |
|---|
| 136 | return title; |
|---|
| 137 | } |
|---|
| 138 | |
|---|
| 139 | public void setTitle(String title) |
|---|
| 140 | { |
|---|
| 141 | this.title = title; |
|---|
| 142 | } |
|---|
| 143 | |
|---|
| 144 | public String getUrl() |
|---|
| 145 | { |
|---|
| 146 | return url; |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | public void setUrl(String url) |
|---|
| 150 | { |
|---|
| 151 | this.url = url; |
|---|
| 152 | } |
|---|
| 153 | |
|---|
| 154 | public String getUser() |
|---|
| 155 | { |
|---|
| 156 | return user; |
|---|
| 157 | } |
|---|
| 158 | |
|---|
| 159 | public void setUser(String user) |
|---|
| 160 | { |
|---|
| 161 | this.user = user; |
|---|
| 162 | } |
|---|
| 163 | |
|---|
| 164 | public String getPassword() |
|---|
| 165 | { |
|---|
| 166 | return password; |
|---|
| 167 | } |
|---|
| 168 | |
|---|
| 169 | public void setPassword(String password) |
|---|
| 170 | { |
|---|
| 171 | this.password = password; |
|---|
| 172 | } |
|---|
| 173 | |
|---|
| 174 | public String getQuery() |
|---|
| 175 | { |
|---|
| 176 | return query; |
|---|
| 177 | } |
|---|
| 178 | |
|---|
| 179 | public void setQuery(String query) |
|---|
| 180 | { |
|---|
| 181 | this.query = query; |
|---|
| 182 | } |
|---|
| 183 | |
|---|
| 184 | public HashMap<String, Parameter> getParameters() |
|---|
| 185 | { |
|---|
| 186 | return parameters; |
|---|
| 187 | } |
|---|
| 188 | |
|---|
| 189 | public Hashtable<String, Service> getServices() |
|---|
| 190 | { |
|---|
| 191 | return services; |
|---|
| 192 | } |
|---|
| 193 | |
|---|
| 194 | public void setServices(Hashtable<String, Service> services) |
|---|
| 195 | { |
|---|
| 196 | this.services = services; |
|---|
| 197 | } |
|---|
| 198 | |
|---|
| 199 | public String getType() |
|---|
| 200 | { |
|---|
| 201 | return type; |
|---|
| 202 | } |
|---|
| 203 | |
|---|
| 204 | @SuppressWarnings("unchecked") |
|---|
| 205 | public ObjectPool getPool() |
|---|
| 206 | { |
|---|
| 207 | return pool; |
|---|
| 208 | } |
|---|
| 209 | |
|---|
| 210 | } |
|---|