Skip to content

Commit 97f0514

Browse files
author
ehennum
committed
Bug:25862 shortcut for raw IO object or class when handle is registered
git-svn-id: svn+ssh://svn.marklogic.com/project/engsvn/client-api/java/branches/b2_0@161848 62cac252-8da6-4816-9e9d-6dc37b19578c
1 parent 51995b5 commit 97f0514

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+3418
-395
lines changed

src/main/java/com/marklogic/client/DatabaseClientFactory.java

Lines changed: 152 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package com.marklogic.client;
1717

1818
import java.io.Serializable;
19+
import java.util.Set;
1920

2021
import javax.net.ssl.SSLContext;
2122
import javax.net.ssl.SSLException;
@@ -25,7 +26,10 @@
2526

2627
import com.marklogic.client.extra.httpclient.HttpClientConfigurator;
2728
import com.marklogic.client.impl.DatabaseClientImpl;
29+
import com.marklogic.client.impl.HandleFactoryRegistryImpl;
2830
import com.marklogic.client.impl.JerseyServices;
31+
import com.marklogic.client.io.marker.ContentHandle;
32+
import com.marklogic.client.io.marker.ContentHandleFactory;
2933

3034
/**
3135
* A Database Client Factory configures a database client for making
@@ -35,6 +39,8 @@ public class DatabaseClientFactory {
3539
static final private Logger logger = LoggerFactory.getLogger(DatabaseClientFactory.class);
3640

3741
static private ClientConfigurator<?> clientConfigurator;
42+
static private HandleFactoryRegistry handleRegistry =
43+
HandleFactoryRegistryImpl.newDefault();
3844

3945
/**
4046
* Authentication enumerates the methods for verifying a user and
@@ -116,6 +122,64 @@ public String getName() {
116122
}
117123
}
118124

125+
/**
126+
* A HandleFactoryRegistry associates IO representation classes
127+
* with handle factories. The API uses the registry to create
128+
* a content handle to act as an adapter for a supported
129+
* IO representation class. The IO class and its instances can
130+
* then be passed directly to convenience methods.
131+
* The default registry associates the content handles provided
132+
* by the API and the supported IO representation classes.
133+
* Create instances of this interface only if you need to modify
134+
* the default registry.
135+
* @see DatabaseClientFactory#getHandleRegistry()
136+
* @see DatabaseClientFactory.Bean#getHandleRegistry()
137+
*/
138+
public interface HandleFactoryRegistry {
139+
/**
140+
* Associates a factory for content handles with the classes
141+
* for IO representations known to the factory.
142+
* @param factory a factory for creating content handle instances
143+
*/
144+
public void register(ContentHandleFactory factory);
145+
/**
146+
* Associates a factory for content handles with the specified classes
147+
* for IO representations.
148+
* @param factory a factory for creating content handle instances
149+
* @param ioClasses the IO classes for which the factory should create handles
150+
*/
151+
public void register(ContentHandleFactory factory, Class<?>... ioClasses);
152+
/**
153+
* Returns whether the registry associates the class with a factory.
154+
* @param ioClass the class for an IO representation
155+
* @return true if a factory has been registered
156+
*/
157+
public boolean isRegistered(Class<?> ioClass);
158+
/**
159+
* Returns the classes for the IO representations for which a factory
160+
* has been registered.
161+
* @return the IO classes
162+
*/
163+
public Set<Class<?>> listRegistered();
164+
/**
165+
* Creates a ContentHandle if the registry has a factory
166+
* for the class of the IO representation.
167+
* @param type the class for an IO representation
168+
* @return a content handle or null if no factory supports the class
169+
*/
170+
public <C> ContentHandle<C> makeHandle(Class<C> type);
171+
/**
172+
* Removes the classes from the registry
173+
* @param ioClasses one or more registered classes for an IO representation
174+
*/
175+
public void unregister(Class<?>... ioClasses);
176+
/**
177+
* Create a copy of the current registry
178+
* @return a copy of the current registry
179+
*/
180+
public HandleFactoryRegistry copy();
181+
}
182+
119183
/**
120184
* A ClientConfigurator provides custom configuration for the communication library
121185
* used to sending client requests and receiving server responses.
@@ -133,22 +197,6 @@ public interface ClientConfigurator<T> {
133197
private DatabaseClientFactory() {
134198
}
135199

136-
/**
137-
* Adds a listener that provides custom configuration when a communication library
138-
* is created.
139-
* @see com.marklogic.client.extra.httpclient.HttpClientConfigurator
140-
* @param configurator the listener for configuring the communication library
141-
*/
142-
static public void addConfigurator(ClientConfigurator<?> configurator) {
143-
if (!HttpClientConfigurator.class.isInstance(configurator)) {
144-
throw new IllegalArgumentException(
145-
"Configurator must implement HttpClientConfigurator"
146-
);
147-
}
148-
149-
clientConfigurator = configurator;
150-
}
151-
152200
/**
153201
* Creates a client to access the database by means of a REST server
154202
* without any authentication. Such clients can be convenient for
@@ -183,7 +231,7 @@ static public DatabaseClient newClient(String host, int port, String user, Strin
183231
* @param user the user with read, write, or administrative privileges
184232
* @param password the password for the user
185233
* @param type the type of authentication applied to the request
186-
* @param context the SSL content for authenticating with the server
234+
* @param context the SSL context for authenticating with the server
187235
* @return a new client for making database requests
188236
*/
189237
static public DatabaseClient newClient(String host, int port, String user, String password, Authentication type, SSLContext context) {
@@ -197,11 +245,16 @@ static public DatabaseClient newClient(String host, int port, String user, Strin
197245
* @param user the user with read, write, or administrative privileges
198246
* @param password the password for the user
199247
* @param type the type of authentication applied to the request
200-
* @param context the SSL content for authenticating with the server
248+
* @param context the SSL context for authenticating with the server
201249
* @param verifier a callback for checking hostnames
202250
* @return a new client for making database requests
203251
*/
204252
static public DatabaseClient newClient(String host, int port, String user, String password, Authentication type, SSLContext context, SSLHostnameVerifier verifier) {
253+
DatabaseClientImpl client = newClientImpl(host, port, user, password, type, context, verifier);
254+
client.setHandleRegistry(getHandleRegistry().copy());
255+
return client;
256+
}
257+
static private DatabaseClientImpl newClientImpl(String host, int port, String user, String password, Authentication type, SSLContext context, SSLHostnameVerifier verifier) {
205258
logger.debug("Creating new database client for server at "+host+":"+port);
206259
JerseyServices services = new JerseyServices();
207260
services.connect(host, port, user, password, type, context, verifier);
@@ -215,6 +268,48 @@ static public DatabaseClient newClient(String host, int port, String user, Strin
215268
return new DatabaseClientImpl(services);
216269
}
217270

271+
/**
272+
* Returns the default registry with factories for creating handles
273+
* as adapters for IO representations. To create custom registries,
274+
* use
275+
* @return the default registry
276+
*/
277+
static public HandleFactoryRegistry getHandleRegistry() {
278+
return handleRegistry;
279+
}
280+
/**
281+
* Removes the current registered associations so the
282+
* handle registry is empty.
283+
*/
284+
static public void clearHandleRegistry() {
285+
handleRegistry = new HandleFactoryRegistryImpl();
286+
}
287+
/**
288+
* Initializes a handle registry with the default associations
289+
* between the content handles provided by the API and the supported
290+
* IO representation classes. Use this method only after clearing
291+
* or overwriting associations in the handle registry.
292+
*/
293+
static public void registerDefaultHandles() {
294+
HandleFactoryRegistryImpl.registerDefaults(getHandleRegistry());
295+
}
296+
297+
/**
298+
* Adds a listener that provides custom configuration when a communication library
299+
* is created.
300+
* @see com.marklogic.client.extra.httpclient.HttpClientConfigurator
301+
* @param configurator the listener for configuring the communication library
302+
*/
303+
static public void addConfigurator(ClientConfigurator<?> configurator) {
304+
if (!HttpClientConfigurator.class.isInstance(configurator)) {
305+
throw new IllegalArgumentException(
306+
"Configurator must implement HttpClientConfigurator"
307+
);
308+
}
309+
310+
clientConfigurator = configurator;
311+
}
312+
218313
/**
219314
* A Database Client Factory Bean provides an object for specifying configuration
220315
* before creating a client to make database requests.
@@ -240,13 +335,16 @@ static public DatabaseClient newClient(String host, int port, String user, Strin
240335
static public class Bean implements Serializable {
241336
private static final long serialVersionUID = 1L;
242337

243-
private String host;
244-
private int port;
245-
private String user;
246-
private String password;
247-
private Authentication authentication;
248-
transient private SSLContext context;
249-
transient private SSLHostnameVerifier verifier;
338+
private String host;
339+
private int port;
340+
private String user;
341+
private String password;
342+
private Authentication authentication;
343+
private HandleFactoryRegistry handleRegistry =
344+
HandleFactoryRegistryImpl.newDefault();
345+
346+
transient private SSLContext context;
347+
transient private SSLHostnameVerifier verifier;
250348

251349
/**
252350
* Zero-argument constructor for bean applications. Other
@@ -379,6 +477,31 @@ public void setVerifier(SSLHostnameVerifier verifier) {
379477
this.verifier = verifier;
380478
}
381479

480+
/**
481+
* Returns the registry for associating
482+
* IO representation classes with handle factories.
483+
* @return the registry instance
484+
*/
485+
public HandleFactoryRegistry getHandleRegistry() {
486+
return handleRegistry;
487+
}
488+
/**
489+
* Removes the current registered associations so the
490+
* handle registry is empty.
491+
*/
492+
public void clearHandleRegistry() {
493+
this.handleRegistry = new HandleFactoryRegistryImpl();
494+
}
495+
/**
496+
* Initializes a handle registry with the default associations
497+
* between the content handles provided by the API and the supported
498+
* IO representation classes. Use this method only after clearing
499+
* or overwriting associations in the handle registry.
500+
*/
501+
public void registerDefaultHandles() {
502+
HandleFactoryRegistryImpl.registerDefaults(getHandleRegistry());
503+
}
504+
382505
/**
383506
* Creates a client for bean applications based on the properties.
384507
* Other applications can use the static newClient() factory methods
@@ -387,9 +510,10 @@ public void setVerifier(SSLHostnameVerifier verifier) {
387510
* @return a new client for making database requests
388511
*/
389512
public DatabaseClient newClient() {
390-
return DatabaseClientFactory.newClient(
391-
host, port, user, password, authentication, context, verifier
392-
);
513+
DatabaseClientImpl client = newClientImpl(host, port, user, password, authentication, context, verifier);
514+
client.setHandleRegistry(getHandleRegistry().copy());
515+
516+
return client;
393517
}
394518
}
395519
}

src/main/java/com/marklogic/client/admin/ExtensionLibrariesManager.java

Lines changed: 64 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,68 +29,112 @@
2929
*
3030
*/
3131
public interface ExtensionLibrariesManager {
32-
3332
/**
3433
* Lists all of the library files that are installed on the server.
3534
* @return An array of ExtensionLibraryDescriptor objects.
3635
*/
3736
public ExtensionLibraryDescriptor[] list()
38-
throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException;
39-
37+
throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException;
4038
/**
4139
* Lists all of the library files in one directory (infinite depth) on the server.
4240
* @param directory The directory to list.
4341
* @return An array of ExtensionLibraryDescriptor objects.
4442
*/
4543
public ExtensionLibraryDescriptor[] list(String directory)
46-
throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException;
44+
throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException;
45+
46+
/**
47+
* Reads the contents of a library asset as an object of an IO class.
48+
*
49+
* The IO class must have been registered before creating the database client.
50+
* By default, standard Java IO classes for document content are registered.
51+
*
52+
* @param libraryPath the path to the library
53+
* @param as the IO class for reading the library asset
54+
* @return an object of the IO class with the library asset
55+
*/
56+
public <T> T readAs(String libraryPath, Class<T> as)
57+
throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException;
58+
/**
59+
* Reads the contents of a library asset as an object of an IO class.
60+
*
61+
* The IO class must have been registered before creating the database client.
62+
* By default, standard Java IO classes for document content are registered.
63+
*
64+
* @param libraryDescriptor a descriptor that locates the library
65+
* @param as the IO class for reading the library asset
66+
* @return an object of the IO class with the library asset
67+
*/
68+
public <T> T read(ExtensionLibraryDescriptor libraryDescriptor, Class<T> as)
69+
throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException;
4770

4871
/**
4972
* Reads the contents of a library asset into a handle.
50-
* @param libraryPath The path to the library.
51-
* @param readHandle A handle for reading the contents of the file.
52-
* @return The handle.
73+
* @param libraryPath the path to the library
74+
* @param readHandle a handle for reading the contents of the file
75+
* @return the handle for the library asset
5376
*/
5477
public <T extends AbstractReadHandle> T read(String libraryPath, T readHandle)
55-
throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException;
56-
78+
throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException;
5779
/**
5880
* Reads the contents of a library asset into a handle.
5981
* @param libraryDescriptor a descriptor that locates the library.
6082
* @param readHandle A handle for reading the contents of the file.
6183
* @return The handle.
6284
*/
63-
public <T extends AbstractReadHandle> T read(
64-
ExtensionLibraryDescriptor libraryDescriptor, T readHandle)
65-
throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException;
85+
public <T extends AbstractReadHandle> T read(ExtensionLibraryDescriptor libraryDescriptor, T readHandle)
86+
throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException;
87+
88+
/**
89+
* Writes the contents of a handle to the provided path on the REST server
90+
* as an object of an IO class.
91+
*
92+
* The IO class must have been registered before creating the database client.
93+
* By default, standard Java IO classes for document content are registered.
94+
*
95+
* @param libraryPath The path at which to install the library.
96+
* @param content an IO representation of the library asset
97+
*/
98+
public void writeAs(String libraryPath, Object content)
99+
throws ResourceNotFoundException, ResourceNotResendableException, ForbiddenUserException, FailedRequestException;
100+
/**
101+
* Writes the contents of a handle to the provided path on the REST server
102+
* as an object of an IO class.
103+
*
104+
* The IO class must have been registered before creating the database client.
105+
* By default, standard Java IO classes for document content are registered.
106+
*
107+
* @param libraryDescriptor The descriptory which locates where to install the library.
108+
* @param content an IO representation of the library asset
109+
*/
110+
public void writeAs(ExtensionLibraryDescriptor libraryDescriptor, Object content)
111+
throws ResourceNotFoundException, ResourceNotResendableException, ForbiddenUserException, FailedRequestException;
66112

67113
/**
68114
* Writes the contents of a handle to the provided path on the REST server.
69115
* @param libraryPath The path at which to install the library.
70116
* @param contentHandle The handle containing the contents of the library.
71117
*/
72118
public void write(String libraryPath, AbstractWriteHandle contentHandle)
73-
throws ResourceNotFoundException, ResourceNotResendableException, ForbiddenUserException, FailedRequestException;
74-
119+
throws ResourceNotFoundException, ResourceNotResendableException, ForbiddenUserException, FailedRequestException;
75120
/**
76121
* Writes the contents of a handle to the provided path on the REST server.
77122
* @param libraryDescriptor The descriptory which locates where to install the library.
78123
* @param contentHandle The handle containing the contents of the library.
79124
*/
80125
public void write(ExtensionLibraryDescriptor libraryDescriptor, AbstractWriteHandle contentHandle)
81-
throws ResourceNotFoundException, ResourceNotResendableException, ForbiddenUserException, FailedRequestException;
126+
throws ResourceNotFoundException, ResourceNotResendableException, ForbiddenUserException, FailedRequestException;
82127

83128
/**
84129
* Removes a library asset from the server.
85130
* @param libraryPath The path to the library to delete.
86131
*/
87132
public void delete(String libraryPath)
88-
throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException;
89-
133+
throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException;
90134
/**
91135
* Removes a library asset from the server.
92-
* @param modulesDescriptor A descriptor locating the library to delete.
136+
* @param libraryDescriptor A descriptor locating the library to delete.
93137
*/
94-
public void delete(ExtensionLibraryDescriptor modulesDescriptor)
95-
throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException;
138+
public void delete(ExtensionLibraryDescriptor libraryDescriptor)
139+
throws ResourceNotFoundException, ForbiddenUserException, FailedRequestException;
96140
}

0 commit comments

Comments
 (0)