01: /*
02: * Copyright 2001 Sun Microsystems, Inc. All rights reserved.
03: * PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
04: */
05: package com.sun.portal.rewriter.services;
06:
07: import com.sun.portal.rewriter.services.idsame.IDSAMEDataService;
08: import com.sun.portal.rewriter.util.Constants;
09: import com.sun.portal.rewriter.util.Debug;
10: import com.sun.portal.rewriter.util.StringHelper;
11:
12: import java.lang.reflect.Constructor;
13: import java.util.Properties;
14:
15: /**
16: * Factory to get the instance of DataService implementation
17: *
18: * @version 1.0 12/15/2001
19: * @author Raja Nagendra Kumar, Nagendra.Raja@sun.com
20: */
21: public final class DataServiceFactory {
22: private static final String SERVICE_PACKAGE_PREFIX = "com.sun.portal.rewriter.services.";
23: private static final String IDS_IMPL = SERVICE_PACKAGE_PREFIX
24: + "ids.IDSDataService";
25: private static final String FILE_IMPL = SERVICE_PACKAGE_PREFIX
26: + "file.FileDataService";
27:
28: public static DataService create(final Properties aDataServiceProps) {
29: //assert aDataServiceProps != null;
30: final String lType = StringHelper.normalize(aDataServiceProps
31: .getProperty(Constants.PROPERTY_DATA_SOURCE_TYPE));
32: final DataService dataService;
33:
34: if (lType.equals(DataService.IDS)) {
35: dataService = createServiceUsingReflection(IDS_IMPL,
36: aDataServiceProps);
37: } else if (lType.equals(Constants.FILE)) {
38: dataService = createServiceUsingReflection(FILE_IMPL,
39: aDataServiceProps);
40: } else if (lType.equals(DataService.CUSTOM)) {
41: dataService = createServiceUsingReflection(
42: aDataServiceProps
43: .getProperty(DataService.PROPERTY_CUSTOM_DATA_SERVICE_IMPLEMENTOR),
44: aDataServiceProps);
45: } else //IDSAME is the default
46: {
47: dataService = new IDSAMEDataService(aDataServiceProps);
48: }
49: return dataService;
50: }//create()
51:
52: /**
53: This method is written to overcome the compile time and runtime problems
54: associated with the testServices such as File and IDS
55: */
56: private static DataService createServiceUsingReflection(
57: final String aClassName, final Properties aProps) {
58: System.out
59: .println("Data Service Provider Class: " + aClassName);
60: try {
61: Class classRef = Class.forName(aClassName);
62: Constructor constructor = classRef
63: .getConstructor(new Class[] { Properties.class });
64: return (DataService) constructor
65: .newInstance(new Object[] { aProps });
66: } catch (Exception e) {
67: System.out.println("Failed to create the DataService : "
68: + aClassName + "\n Exception Was : "
69: + StringHelper.exceptionStack2String(e));
70: throw new RuntimeException(StringHelper
71: .exceptionStack2String(e));
72: }
73: }//createServiceUsingReflection
74:
75: public static void main(String[] args) throws DataServiceException {
76: //args[0] - base direcotry name
77: //args[1] - file name
78:
79: String lBaseDir = args[0];
80: String lFileName = args[1];
81: Properties props = new Properties();
82: props.setProperty(Constants.PROPERTY_DATA_SOURCE_TYPE, ""
83: + Constants.FILE);
84: props.setProperty(DataService.PROPERTY_DATA_SERVICE_BASE,
85: lBaseDir);
86:
87: DataService dataService = DataServiceFactory.create(props);
88:
89: Debug.println(dataService.storeXML(lFileName, "one junk data"));
90: Debug.println(dataService.retrieveKeys());
91: Debug.println(dataService.storeXML(lFileName,
92: "two raja nagendra kumar"));
93: Debug.println(dataService.storeXML(lFileName + lFileName,
94: "two raja nagendra kumar"));
95: Debug.println(dataService.deleteKey(lFileName));
96: }//main()
97:
98: }//class DataServiceFactory
|