001: package net.bagaluten.jca.lucene.connector.impl;
002:
003: import java.io.PrintWriter;
004: import java.util.Set;
005: import java.util.logging.Logger;
006:
007: import javax.resource.ResourceException;
008: import javax.resource.spi.ConnectionManager;
009: import javax.resource.spi.ConnectionRequestInfo;
010: import javax.resource.spi.ManagedConnection;
011: import javax.resource.spi.ManagedConnectionFactory;
012: import javax.security.auth.Subject;
013:
014: public class LuceneManagedConnectionFactory implements
015: ManagedConnectionFactory {
016:
017: private static final long serialVersionUID = -5795350364827952255L;
018:
019: private static final Logger log = Logger
020: .getLogger(LuceneManagedConnectionFactory.class.getName());
021:
022: /** Log writer */
023: private transient PrintWriter writer;
024:
025: /** Path to file system repository */
026: private String repository;
027:
028: /*
029: * @see javax.resource.spi.ManagedConnectionFactory#createConnectionFactory(javax.resource.spi.ConnectionManager)
030: */
031: public Object createConnectionFactory(ConnectionManager cm)
032: throws ResourceException {
033: return new LuceneConnectionFactoryImpl(this , cm);
034: }
035:
036: /*
037: * @see javax.resource.spi.ManagedConnectionFactory#createConnectionFactory()
038: */
039: public Object createConnectionFactory() throws ResourceException {
040: return new LuceneConnectionFactoryImpl(this , null);
041: }
042:
043: /*
044: * @see javax.resource.spi.ManagedConnectionFactory#createManagedConnection(javax.security.auth.Subject,
045: * javax.resource.spi.ConnectionRequestInfo)
046: */
047: public ManagedConnection createManagedConnection(Subject subj,
048: ConnectionRequestInfo conReqInfo) throws ResourceException {
049: if (conReqInfo instanceof LuceneConnectionRequestInfo) {
050: LuceneConnectionRequestInfo cri = (LuceneConnectionRequestInfo) conReqInfo;
051: log.fine("creating new manageged connection for index "
052: + cri.getIndexName());
053: } else {
054: log.fine("creating new unconfigured managed connection.");
055: }
056: return new LuceneManagedConnection(conReqInfo, writer,
057: repository);
058: }
059:
060: /*
061: * @see javax.resource.spi.ManagedConnectionFactory#matchManagedConnections(java.util.Set,
062: * javax.security.auth.Subject,
063: * javax.resource.spi.ConnectionRequestInfo)
064: */
065: public ManagedConnection matchManagedConnections(Set set,
066: Subject subj, ConnectionRequestInfo conReqInfo)
067: throws ResourceException {
068: log.info("trying to match a managed connection");
069: if (conReqInfo == null
070: || !(conReqInfo instanceof LuceneConnectionRequestInfo)) {
071: log
072: .severe("internal error - invalid request info parameter");
073: throw new ResourceException(
074: "invalid request info parameter");
075: }
076:
077: for (Object obj : set) {
078: if (obj instanceof LuceneManagedConnection) {
079: LuceneManagedConnection mc = (LuceneManagedConnection) obj;
080: String idx = mc.getIndex();
081: if (idx == null) {
082: log
083: .fine("found unconfigured connection - configuring it.");
084: mc.setIndex(conReqInfo);
085: return mc;
086: }
087: if (mc.matches(conReqInfo)) {
088: log.fine("found matching connection for index '"
089: + idx + "'.");
090: return (LuceneManagedConnection) obj;
091: }
092: }
093: }
094: return null;
095: }
096:
097: /*
098: * @see javax.resource.spi.ManagedConnectionFactory#setLogWriter(java.io.PrintWriter)
099: */
100: public void setLogWriter(PrintWriter out) throws ResourceException {
101: this .writer = out;
102: }
103:
104: /*
105: * @see javax.resource.spi.ManagedConnectionFactory#getLogWriter()
106: */
107: public PrintWriter getLogWriter() throws ResourceException {
108: return writer;
109: }
110:
111: /*
112: * @see javax.resource.spi.ManagedConnectionFactory#hashCode()
113: */
114: public int hashCode() {
115: if (repository == null) {
116: return super .hashCode();
117: } else {
118: return repository.hashCode();
119: }
120: }
121:
122: /*
123: * @see javax.resource.spi.ManagedConnectionFactory#equals(java.lang.Object)
124: */
125: public boolean equals(Object obj) {
126: if (this == obj) {
127: return true;
128: }
129: if (!(obj instanceof LuceneManagedConnectionFactory)) {
130: return false;
131: }
132: if (repository == null) {
133: return false;
134: }
135: return repository.equals(((LuceneManagedConnectionFactory) obj)
136: .getRepositoryPath());
137: }
138:
139: /**
140: * Returns path to file system repository
141: *
142: * @return path to file system repository
143: */
144: public String getRepositoryPath() {
145: return repository;
146: }
147:
148: /**
149: * Sets path to file system repository
150: *
151: * @param path
152: * path to file system repository
153: */
154: public void setRepositoryPath(String path) {
155: this.repository = path;
156: }
157:
158: }
|