Source code: org/media/datastore/sepengine/driver/beepDriver/BeepDriver.java
1 /*
2 * $COPYRIGHT$
3 * $Id: BeepDriver.java,v 1.5 2002/03/20 17:23:18 atech Exp $
4 *
5 * Date Author Changes
6 * Jun 28 2001 Szabo Csaba Created
7 */
8
9 package org.media.datastore.sepengine.driver.beepDriver;
10
11 import java.util.Properties;
12 import java.util.StringTokenizer;
13 import org.media.datastore.sepengine.driver.*;
14
15 /**
16 * @author <a href="mailto:crow@nolimits.ro">Szabo Csaba </a>
17 * @version $Revision: 1.5 $ $Date: 2001/08/21 15:00:00
18 * @see java.lang.String
19 * @see java.util.Properties
20 * @see org.media.datastore.sepengine.driver.SepStoreDriver
21 */
22
23 public class BeepDriver
24 implements org.media.datastore.sepengine.driver.SepStoreDriver {
25
26
27 public BeepDriver() throws Exception {
28 }
29
30
31 public boolean acceptsURL(String url) throws Exception {
32 return parseURL(url, null) != null;
33 }
34
35
36 public SepStoreConnection connect(String url, Properties info) throws Exception {
37 if ( ( props = parseURL( url, info ) ) == null ) return null;
38 try {
39 BeepConnection con = ( BeepConnection )Class.forName(BeepDriverClass.connectClass).newInstance();
40 con.openConnection( host(), port(), props, database(), url, this );
41 return (BeepConnection)con;
42 }
43 catch ( Exception e ) {
44 throw e;
45 }
46 }
47
48
49 public String database() {
50 return props.getProperty("SEPDBNAME");
51 }
52
53
54 public String host() {
55 return props.getProperty("SEPHOST", "localhost");
56 }
57
58
59 public int port() {
60 return Integer.parseInt(props.getProperty("SEPPORT", "10288"));
61 }
62
63
64 public SepStoreDriverPropertyInfo[] getPropertyInfo( String url, Properties info )
65 throws Exception {
66 Properties prop = parseURL(url, info);
67 SepStoreDriverPropertyInfo dpi[] = new SepStoreDriverPropertyInfo[0];
68 return dpi;
69 }
70
71
72 public String property( String name ) {
73 return props.getProperty(name);
74 }
75
76
77 // parse URL and add SEP database name to properties
78 Properties parseURL(String url, Properties defaults) throws Exception {
79 int state = -1;
80 Properties urlProps = new Properties(defaults);
81 String key = new String();
82 String value = new String();
83 StringTokenizer st = new StringTokenizer ( url, ":/;?&=#", true );
84
85 for ( int count = 0; st.hasMoreTokens() ; count++ ) {
86 String token = st.nextToken();
87
88 if ( count <= 3 ) {
89 if ( count % 2 == 0 || !token.equals(":") ) {
90 if ( count % 2 == 0 ) {
91 if ( count == 0 && !token.equals( protocols[0] ) ) return null;
92 if ( count == 2 && !token.equals( protocols[1] ) ) return null;
93 }
94 else {
95 return null;
96 }
97 }
98 }
99 else {
100 if ( count == 4 && token.equals("/") ) {
101 state = 0;
102 }
103 else {
104 if ( count == 4 ) {
105 urlProps.put("SEPDBNAME", token);
106 state = -2;
107 }
108 else {
109 if ( count == 5 && state == 0 && token.equals("/") ) {
110 state = 1;
111 }
112 else {
113 if ( count == 5 && state == 0 ) return null;
114 if ( count == 6 && state == 1 ) {
115 urlProps.put("SEPHOST", token);
116 }
117 else {
118 if ( count == 7 && token.equals(":") ) {
119 state = 2;
120 }
121 else {
122 if ( count == 8 && state == 2 ) {
123 try {
124 Integer portNumber = Integer.decode(token);
125 urlProps.put("SEPPORT", portNumber.toString());
126 }
127 catch ( Exception _ex ) {
128 return null;
129 }
130 }
131 else {
132 if ( ( count == 7 || count == 9 ) && ( state == 1 || state == 2 ) && token.equals("/") ) {
133 state = -1;
134 }
135 else {
136 if ( state == -1 ) {
137 urlProps.put("SEPDBNAME", token);
138 state = -2;
139 }
140 else {
141 if ( state <= -2 && count % 2 == 1 ) {
142 if ( token.equals(";") || token.equals("?") || token.equals("&") ) {
143 state = -3;
144 }
145 else {
146 if ( token.equals("=") ) state = -5;
147 }
148 }
149 else {
150 if ( state <= -2 && count % 2 == 0 ) {
151 if ( state == -3 ) {
152 key = token;
153 }
154 else {
155 if ( state == -5 ) {
156 value = token;
157 urlProps.put(key, value);
158 state = -2;
159 }
160 }
161 }
162 }
163 }
164 }
165 }
166 }
167 }
168 }
169 }
170 }
171 }
172 }
173
174 return urlProps;
175 }
176
177
178 private Properties props;
179 private static String protocols[] = {"sep", "beep"};
180
181
182 static {
183 try {
184 SepStoreDriverManager.registerDriver( new BeepDriver() );
185 }
186 catch ( Exception e ) {
187 e.printStackTrace();
188 }
189 }
190 }