Author: rhauch
Date: 2009-05-21 15:03:27 -0400 (Thu, 21 May 2009)
New Revision: 920
Removed:
trunk/dna-repository/src/main/java/org/jboss/dna/repository/mimetype/MimeType.java
trunk/dna-repository/src/test/java/org/jboss/dna/repository/mimetype/MimeTypeTest.java
Modified:
trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/JcrEngine.java
trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/JcrConfigurationTest.java
trunk/dna-repository/src/main/java/org/jboss/dna/repository/Configurator.java
trunk/dna-repository/src/main/java/org/jboss/dna/repository/DnaEngine.java
trunk/dna-repository/src/main/java/org/jboss/dna/repository/RepositoryLibrary.java
trunk/dna-repository/src/main/java/org/jboss/dna/repository/RepositoryService.java
trunk/dna-repository/src/main/java/org/jboss/dna/repository/mimetype/MimeTypeDetectorConfig.java
trunk/dna-repository/src/main/java/org/jboss/dna/repository/mimetype/MimeTypeDetectors.java
trunk/dna-repository/src/main/java/org/jboss/dna/repository/sequencer/StreamSequencerAdapter.java
trunk/dna-repository/src/test/java/org/jboss/dna/repository/DnaEngineTest.java
trunk/dna-repository/src/test/java/org/jboss/dna/repository/RepositoryServiceTest.java
trunk/dna-repository/src/test/java/org/jboss/dna/repository/mimetype/AbstractMimeTypeTest.java
trunk/dna-repository/src/test/java/org/jboss/dna/repository/sequencer/StreamSequencerAdapterTest.java
Log:
DNA-404 Remove ObservationService, migrate usage to observe RepositoryLibrary
Began the removal of ObservationService by first simplifying DnaEngine initialization.
Changed how the MIME type detectors are managed (no longer in RepositoryLibrary, which
seemed the wrong place, and now in the DnaEngine proper), exposing the MIME type detectors
via the DnaEngine's ExecutionContext. Also removed the MimeType singleton class to
ensure that all detection is done via the (existing)
ExecutionContext.getMimeTypeDetector().mimeTypeOf(...) approach.
Modified: trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/JcrEngine.java
===================================================================
--- trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/JcrEngine.java 2009-05-21 17:01:04 UTC
(rev 919)
+++ trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/JcrEngine.java 2009-05-21 19:03:27 UTC
(rev 920)
@@ -25,6 +25,7 @@
import java.util.HashMap;
import java.util.Map;
+import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import javax.jcr.Repository;
@@ -44,7 +45,6 @@
import org.jboss.dna.jcr.JcrRepository.Option;
import org.jboss.dna.repository.DnaEngine;
import org.jboss.dna.repository.RepositoryService;
-import org.jboss.dna.repository.observation.ObservationService;
import org.jboss.dna.repository.sequencer.SequencingService;
/**
@@ -98,10 +98,6 @@
return dnaEngine.getRepositoryService();
}
- protected final ObservationService getObservationService() {
- return dnaEngine.getObservationService();
- }
-
protected final SequencingService getSequencingService() {
return dnaEngine.getSequencingService();
}
@@ -167,8 +163,8 @@
Node optionNode =
configuration.getNodeAt(optionLocation);
Property valueProperty =
optionNode.getProperty(DnaLexicon.VALUE);
-
options.put(Option.valueOf(segment.getName().getLocalName()),
- valueProperty.getFirstValue().toString());
+
options.put(Option.valueOf(segment.getName().getLocalName()),
valueProperty.getFirstValue()
+
.toString());
}
@@ -187,11 +183,40 @@
* Lifecycle methods
*/
+ /**
+ * Start this engine to make it available for use.
+ *
+ * @throws IllegalStateException if this method is called when already shut down.
+ * @see #shutdown()
+ */
public void start() {
dnaEngine.start();
}
+ /**
+ * Shutdown this engine to close all connections, terminate any ongoing background
operations (such as sequencing), and
+ * reclaim any resources that were acquired by this engine. This method may be called
multiple times, but only the first time
+ * has an effect.
+ *
+ * @see #start()
+ */
public void shutdown() {
dnaEngine.shutdown();
}
+
+ /**
+ * Blocks until the shutdown has completed, or the timeout occurs, or the current
thread is interrupted, whichever happens
+ * first.
+ *
+ * @param timeout the maximum time to wait for each component in this engine
+ * @param unit the time unit of the timeout argument
+ * @return <tt>true</tt> if this service complete shut down and
<tt>false</tt> if the timeout elapsed before it was shut down
+ * completely
+ * @throws InterruptedException if interrupted while waiting
+ */
+ public boolean awaitTermination( long timeout,
+ TimeUnit unit ) throws InterruptedException {
+ return dnaEngine.awaitTermination(timeout, unit);
+ }
+
}
Modified: trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/JcrConfigurationTest.java
===================================================================
--- trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/JcrConfigurationTest.java 2009-05-21
17:01:04 UTC (rev 919)
+++ trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/JcrConfigurationTest.java 2009-05-21
19:03:27 UTC (rev 920)
@@ -301,7 +301,7 @@
assertThat(engine.getRepositorySource("config repo"),
is(notNullValue()));
assertThat(engine.getRepositorySource("config repo"),
is(instanceOf(InMemoryRepositorySource.class)));
- RepositoryLibrary library =
engine.getRepositoryService().getRepositorySourceManager();
+ RepositoryLibrary library =
engine.getRepositoryService().getRepositoryLibrary();
assertThat(library.getConnectionPool("config repo").getInUseCount(),
is(0));
RepositoryConnection connection = library.getConnectionPool("config
repo").getConnection();
Modified: trunk/dna-repository/src/main/java/org/jboss/dna/repository/Configurator.java
===================================================================
---
trunk/dna-repository/src/main/java/org/jboss/dna/repository/Configurator.java 2009-05-21
17:01:04 UTC (rev 919)
+++
trunk/dna-repository/src/main/java/org/jboss/dna/repository/Configurator.java 2009-05-21
19:03:27 UTC (rev 920)
@@ -169,6 +169,14 @@
* @return this instance for method chaining purposes; never null
*/
public ConfigRepositoryDetails<ReturnType> under( String path );
+
+ /**
+ * Specify the path under which the configuration content is to be found. This
path is assumed to be "/" by default.
+ *
+ * @param workspace the name of the workspace with the configuration content in
the configuration source; may not be null
+ * @return this instance for method chaining purposes; never null
+ */
+ public ConfigRepositoryDetails<ReturnType> inWorkspace( String workspace
);
}
/**
@@ -454,7 +462,7 @@
protected ConfigurationRepository createDefaultConfigurationSource() {
InMemoryRepositorySource defaultSource = new InMemoryRepositorySource();
defaultSource.setName("Configuration");
- ConfigurationRepository result = new ConfigurationRepository(defaultSource,
"Configuration Repository", null);
+ ConfigurationRepository result = new ConfigurationRepository(defaultSource,
"Configuration Repository", null, null);
return result;
}
@@ -527,7 +535,7 @@
configuration().create(path).with(DnaLexicon.READABLE_NAME, id).and();
return path;
}
-
+
protected void recordBeanPropertiesInGraph( Path path,
Object javaBean ) {
Reflection reflector = new Reflection(javaBean.getClass());
@@ -612,7 +620,7 @@
* @see
org.jboss.dna.repository.Configurator.SetDescription#describedAs(java.lang.String)
*/
public ConfigRepositoryDetails<ReturnType> describedAs( String description
) {
- Configurator.this.configurationSource =
Configurator.this.configurationSource.with(description);
+ Configurator.this.configurationSource =
Configurator.this.configurationSource.withDescription(description);
return this;
}
@@ -630,13 +638,23 @@
/**
* {@inheritDoc}
*
+ * @see
org.jboss.dna.repository.Configurator.ConfigRepositoryDetails#inWorkspace(java.lang.String)
+ */
+ public ConfigRepositoryDetails<ReturnType> inWorkspace( String workspace )
{
+ Configurator.this.configurationSource =
Configurator.this.configurationSource.withWorkspace(workspace);
+ return this;
+ }
+
+ /**
+ * {@inheritDoc}
+ *
* @see
org.jboss.dna.repository.Configurator.ConfigRepositoryDetails#under(java.lang.String)
*/
public ConfigRepositoryDetails<ReturnType> under( String path ) {
CheckArg.isNotNull(path, "path");
Path newPath =
getExecutionContext().getValueFactories().getPathFactory().create(path);
Configurator.this.configurationSource =
Configurator.this.configurationSource.with(newPath);
- return null;
+ return this;
}
/**
@@ -661,7 +679,7 @@
protected final ReturnType returnObject;
public ClassChooser( Path pathOfComponentNode,
- ReturnType returnObject ) {
+ ReturnType returnObject ) {
assert pathOfComponentNode != null;
assert returnObject != null;
this.pathOfComponentNode = pathOfComponentNode;
@@ -846,7 +864,7 @@
protected Path path() {
return this.path;
}
-
+
/**
* {@inheritDoc}
*
@@ -1018,17 +1036,20 @@
private final RepositorySource source;
private final String description;
private final Path path;
+ private final String workspace;
protected ConfigurationRepository( RepositorySource source ) {
- this(source, null, null);
+ this(source, null, null, null);
}
protected ConfigurationRepository( RepositorySource source,
String description,
- Path path ) {
+ Path path,
+ String workspace ) {
this.source = source;
this.description = description != null ? description : "";
this.path = path != null ? path : RootPath.INSTANCE;
+ this.workspace = workspace != null ? workspace : "";
}
/**
@@ -1052,12 +1073,23 @@
return path;
}
- public ConfigurationRepository with( String description ) {
- return new ConfigurationRepository(source, description, path);
+ /**
+ * @return workspace
+ */
+ public String getWorkspace() {
+ return workspace;
}
+ public ConfigurationRepository withDescription( String description ) {
+ return new ConfigurationRepository(source, description, path, workspace);
+ }
+
public ConfigurationRepository with( Path path ) {
- return new ConfigurationRepository(source, description, path);
+ return new ConfigurationRepository(source, description, path, workspace);
}
+
+ public ConfigurationRepository withWorkspace( String workspace ) {
+ return new ConfigurationRepository(source, description, path, workspace);
+ }
}
}
Modified: trunk/dna-repository/src/main/java/org/jboss/dna/repository/DnaEngine.java
===================================================================
--- trunk/dna-repository/src/main/java/org/jboss/dna/repository/DnaEngine.java 2009-05-21
17:01:04 UTC (rev 919)
+++ trunk/dna-repository/src/main/java/org/jboss/dna/repository/DnaEngine.java 2009-05-21
19:03:27 UTC (rev 920)
@@ -22,7 +22,6 @@
package org.jboss.dna.repository;
import java.util.ArrayList;
-import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
@@ -46,16 +45,17 @@
import org.jboss.dna.graph.connector.RepositoryConnectionFactory;
import org.jboss.dna.graph.connector.RepositorySource;
import org.jboss.dna.graph.connector.RepositorySourceException;
+import org.jboss.dna.graph.mimetype.ExtensionBasedMimeTypeDetector;
+import org.jboss.dna.graph.mimetype.MimeTypeDetector;
import org.jboss.dna.graph.property.Name;
import org.jboss.dna.graph.property.Path;
import org.jboss.dna.graph.property.PathExpression;
import org.jboss.dna.graph.property.PathNotFoundException;
import org.jboss.dna.graph.property.Property;
import org.jboss.dna.repository.mimetype.MimeTypeDetectorConfig;
-import org.jboss.dna.repository.observation.ObservationService;
+import org.jboss.dna.repository.mimetype.MimeTypeDetectors;
import org.jboss.dna.repository.sequencer.SequencerConfig;
import org.jboss.dna.repository.sequencer.SequencingService;
-import org.jboss.dna.repository.service.AdministeredService;
/**
* A single instance of the DNA services, which is obtained after setting up the {@link
DnaConfiguration#build() configuration}.
@@ -71,12 +71,11 @@
private final ConfigurationScanner scanner;
private final Problems problems;
private final ExecutionContext context;
- private final List<AdministeredService> services;
private final RepositoryService repositoryService;
- private final ObservationService observationService;
private final SequencingService sequencingService;
private final ExecutorService executorService;
+ private final MimeTypeDetectors detectors;
private final RepositoryConnectionFactory connectionFactory;
@@ -85,40 +84,38 @@
this.problems = new SimpleProblems();
// Use the configuration's context ...
- this.context = context;
+ this.detectors = new MimeTypeDetectors();
+ this.context = context.with(detectors);
// And set up the scanner ...
this.configuration = configuration;
this.scanner = new ConfigurationScanner(this.problems, this.context,
this.configuration);
- // Add the configuration source to the repository library ...
- final RepositorySource configSource = this.configuration.getRepositorySource();
- RepositoryLibrary library = new RepositoryLibrary();
- library.addSource(configSource);
+ // Add the mime type detectors in the configuration ...
+ for (MimeTypeDetectorConfig config : scanner.getMimeTypeDetectors()) {
+ detectors.addDetector(config);
+ }
+ // Add an extension-based detector by default ...
+ detectors.addDetector(new MimeTypeDetectorConfig("ExtensionDetector",
"Extension-based MIME type detector",
+
ExtensionBasedMimeTypeDetector.class));
// Create the RepositoryService, pointing it to the configuration repository ...
Path pathToConfigurationRoot = this.configuration.getPath();
- repositoryService = new RepositoryService(library, configSource.getName(),
"", pathToConfigurationRoot, context);
+ String configWorkspaceName = this.configuration.getWorkspace();
+ final RepositorySource configSource = this.configuration.getRepositorySource();
+ repositoryService = new RepositoryService(configSource, configWorkspaceName,
pathToConfigurationRoot, context);
- for (MimeTypeDetectorConfig config : scanner.getMimeTypeDetectors()) {
- library.getMimeTypeDetectors().addDetector(config);
- }
-
// Create the sequencing service ...
executorService = new ScheduledThreadPoolExecutor(10); // Use a magic number for
now
sequencingService = new SequencingService();
sequencingService.setExecutionContext(context);
sequencingService.setExecutorService(executorService);
-
sequencingService.setRepositoryLibrary(repositoryService.getRepositorySourceManager());
+
sequencingService.setRepositoryLibrary(repositoryService.getRepositoryLibrary());
for (SequencerConfig sequencerConfig : scanner.getSequencingConfigurations()) {
sequencingService.addSequencer(sequencerConfig);
}
- // Create the observation service ...
- observationService = null; // new ObservationService(null);
-
- this.services = Arrays.asList(new AdministeredService[] { /* observationService,
*/repositoryService, sequencingService,});
-
+ // Set up the connection factory for this engine ...
connectionFactory = new RepositoryConnectionFactory() {
public RepositoryConnection createConnection( String sourceName ) throws
RepositorySourceException {
RepositorySource source =
DnaEngine.this.getRepositorySource(sourceName);
@@ -140,48 +137,87 @@
return problems;
}
- /*
- * Lookup methods
+ /**
+ * Get the context in which this engine is executing.
+ *
+ * @return the execution context; never null
*/
public final ExecutionContext getExecutionContext() {
return context;
}
+ /**
+ * Get the {@link RepositorySource} instance used by this engine.
+ *
+ * @param repositoryName the name of the repository source
+ * @return the source, or null if no source with the given name exists
+ */
public final RepositorySource getRepositorySource( String repositoryName ) {
- return repositoryService.getRepositorySourceManager().getSource(repositoryName);
+ return repositoryService.getRepositoryLibrary().getSource(repositoryName);
}
+ /**
+ * Get a factory of connections, backed by the RepositorySor
+ *
+ * @return the connection factory; never null
+ */
public final RepositoryConnectionFactory getRepositoryConnectionFactory() {
return connectionFactory;
}
+ /**
+ * Get the repository service.
+ *
+ * @return the repository service owned by this engine; never null
+ */
public final RepositoryService getRepositoryService() {
return repositoryService;
}
- public final ObservationService getObservationService() {
- return observationService;
- }
-
+ /**
+ * Get the sequencing service.
+ *
+ * @return the sequencing service owned by this engine; never null
+ */
public final SequencingService getSequencingService() {
return sequencingService;
}
+ /**
+ * Return the component that is able to detect MIME types given the name of a stream
and a stream.
+ *
+ * @return the MIME type detector used by this engine; never null
+ */
+ protected final MimeTypeDetector getMimeTypeDetector() {
+ return detectors;
+ }
+
/*
* Lifecycle methods
*/
-
+ /**
+ * Start this engine to make it available for use.
+ *
+ * @throws IllegalStateException if this method is called when already shut down.
+ * @see #shutdown()
+ */
public void start() {
- for (AdministeredService service : services) {
- service.getAdministrator().start();
- }
+ repositoryService.getAdministrator().start();
+ sequencingService.getAdministrator().start();
}
+ /**
+ * Shutdown this engine to close all connections, terminate any ongoing background
operations (such as sequencing), and
+ * reclaim any resources that were acquired by this engine. This method may be called
multiple times, but only the first time
+ * has an effect.
+ *
+ * @see #start()
+ */
public void shutdown() {
- for (AdministeredService service : services) {
- service.getAdministrator().shutdown();
- }
+ // First, shutdown the sequencing service, which will prevent any additional jobs
from going through ...
+ sequencingService.getAdministrator().shutdown();
+ // Then terminate the executor service, which may be running background jobs that
are not yet completed ...
try {
executorService.awaitTermination(10 * 60, TimeUnit.SECONDS); // No
TimeUnit.MINUTES in JDK 5
} catch (InterruptedException ie) {
@@ -189,9 +225,30 @@
Thread.interrupted();
}
executorService.shutdown();
+
+ // Finally shut down the repository source, which closes all connections ...
+ repositoryService.getAdministrator().shutdown();
}
/**
+ * Blocks until the shutdown has completed, or the timeout occurs, or the current
thread is interrupted, whichever happens
+ * first.
+ *
+ * @param timeout the maximum time to wait for each component in this engine
+ * @param unit the time unit of the timeout argument
+ * @return <tt>true</tt> if this service complete shut down and
<tt>false</tt> if the timeout elapsed before it was shut down
+ * completely
+ * @throws InterruptedException if interrupted while waiting
+ */
+ public boolean awaitTermination( long timeout,
+ TimeUnit unit ) throws InterruptedException {
+ if (!sequencingService.getAdministrator().awaitTermination(timeout, unit)) return
false;
+ if (!executorService.awaitTermination(timeout, unit)) return false;
+ if (!repositoryService.getAdministrator().awaitTermination(timeout, unit)) return
false;
+ return true;
+ }
+
+ /**
* The component responsible for reading the configuration repository and
(eventually) for propagating changes in the
* configuration repository into the services.
*/
Modified:
trunk/dna-repository/src/main/java/org/jboss/dna/repository/RepositoryLibrary.java
===================================================================
---
trunk/dna-repository/src/main/java/org/jboss/dna/repository/RepositoryLibrary.java 2009-05-21
17:01:04 UTC (rev 919)
+++
trunk/dna-repository/src/main/java/org/jboss/dna/repository/RepositoryLibrary.java 2009-05-21
19:03:27 UTC (rev 920)
@@ -46,7 +46,6 @@
import org.jboss.dna.graph.observe.Changes;
import org.jboss.dna.graph.observe.Observable;
import org.jboss.dna.graph.observe.Observer;
-import org.jboss.dna.repository.mimetype.MimeTypeDetectors;
import org.jboss.dna.repository.service.AbstractServiceAdministrator;
import org.jboss.dna.repository.service.ServiceAdministrator;
@@ -106,7 +105,6 @@
}
- private final MimeTypeDetectors mimeTypeDetectors = new MimeTypeDetectors();
private final ServiceAdministrator administrator = new Administrator();
private final ReadWriteLock sourcesLock = new ReentrantReadWriteLock();
private final CopyOnWriteArrayList<RepositoryConnectionPool> pools = new
CopyOnWriteArrayList<RepositoryConnectionPool>();
@@ -215,13 +213,6 @@
}
/**
- * @return mimeTypeDetectors
- */
- public MimeTypeDetectors getMimeTypeDetectors() {
- return mimeTypeDetectors;
- }
-
- /**
* Get the delegate connection factory.
*
* @return the connection factory to which this instance should delegate in the event
that a source is not found in this
Modified:
trunk/dna-repository/src/main/java/org/jboss/dna/repository/RepositoryService.java
===================================================================
---
trunk/dna-repository/src/main/java/org/jboss/dna/repository/RepositoryService.java 2009-05-21
17:01:04 UTC (rev 919)
+++
trunk/dna-repository/src/main/java/org/jboss/dna/repository/RepositoryService.java 2009-05-21
19:03:27 UTC (rev 920)
@@ -156,6 +156,35 @@
}
/**
+ * Create a service instance, reading the configuration describing new {@link
RepositorySource} instances from the supplied
+ * configuration repository.
+ *
+ * @param configurationSource the {@link RepositorySource} that is the configuration
repository
+ * @param configurationWorkspaceName the name of the workspace in the {@link
RepositorySource} that is the configuration
+ * repository, or null if the default workspace of the source should be used
(if there is one)
+ * @param pathToConfigurationRoot the path of the node in the configuration source
repository that should be treated by this
+ * service as the root of the service's configuration; if null, then
"/dna:system" is used
+ * @param context the execution context in which this service should run
+ * @throws IllegalArgumentException if the bootstrap source is null or the execution
context is null
+ */
+ public RepositoryService( RepositorySource configurationSource,
+ String configurationWorkspaceName,
+ Path pathToConfigurationRoot,
+ ExecutionContext context ) {
+ CheckArg.isNotNull(configurationSource, "configurationSource");
+ CheckArg.isNotNull(context, "context");
+ if (pathToConfigurationRoot == null) pathToConfigurationRoot =
context.getValueFactories()
+
.getPathFactory()
+
.create("/dna:system");
+ this.sources = new RepositoryLibrary();
+ this.sources.addSource(configurationSource);
+ this.pathToConfigurationRoot = pathToConfigurationRoot;
+ this.configurationSourceName = configurationSource.getName();
+ this.configurationWorkspaceName = configurationWorkspaceName;
+ this.context = context;
+ }
+
+ /**
* {@inheritDoc}
*/
public ServiceAdministrator getAdministrator() {
@@ -177,9 +206,11 @@
}
/**
- * @return sources
+ * Get the library of {@link RepositorySource} instances used by this service.
+ *
+ * @return the RepositorySource library; never null
*/
- public RepositoryLibrary getRepositorySourceManager() {
+ public RepositoryLibrary getRepositoryLibrary() {
return sources;
}
Deleted:
trunk/dna-repository/src/main/java/org/jboss/dna/repository/mimetype/MimeType.java
===================================================================
---
trunk/dna-repository/src/main/java/org/jboss/dna/repository/mimetype/MimeType.java 2009-05-21
17:01:04 UTC (rev 919)
+++
trunk/dna-repository/src/main/java/org/jboss/dna/repository/mimetype/MimeType.java 2009-05-21
19:03:27 UTC (rev 920)
@@ -1,86 +0,0 @@
-/*
- * JBoss DNA (
http://www.jboss.org/dna)
- * See the COPYRIGHT.txt file distributed with this work for information
- * regarding copyright ownership. Some portions may be licensed
- * to Red Hat, Inc. under one or more contributor license agreements.
- * See the AUTHORS.txt file in the distribution for a full listing of
- * individual contributors.
- *
- * JBoss DNA is free software. Unless otherwise indicated, all code in JBoss DNA
- * is licensed to you under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * JBoss DNA is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
- */
-package org.jboss.dna.repository.mimetype;
-
-import java.io.IOException;
-import java.io.InputStream;
-import org.jboss.dna.common.component.ClassLoaderFactory;
-
-/**
- * Convenience class for working with the default {@link MimeTypeDetectors MIME-type
detectors}.
- *
- * @author jverhaeg
- */
-public final class MimeType {
-
- /**
- * The set of
- */
- public static final MimeTypeDetectors DEFAULT_DETECTORS =
MimeTypeDetectors.DEFAULT_DETECTORS;
-
- /**
- * @param config See {@link MimeTypeDetectors#addDetector(MimeTypeDetectorConfig)}.
- * @return See {@link MimeTypeDetectors#addDetector(MimeTypeDetectorConfig)}.
- * @see MimeTypeDetectors#addDetector(MimeTypeDetectorConfig)
- */
- public static boolean addDetector( MimeTypeDetectorConfig config ) {
- return DEFAULT_DETECTORS.addDetector(config);
- }
-
- /**
- * @return See {@link MimeTypeDetectors#getClassLoaderFactory()}.
- * @see MimeTypeDetectors#getClassLoaderFactory()
- */
- public static ClassLoaderFactory getClassLoaderFactory() {
- return DEFAULT_DETECTORS.getClassLoaderFactory();
- }
-
- /**
- * @param name See {@link MimeTypeDetectors#mimeTypeOf(String, InputStream)}.
- * @param content See {@link MimeTypeDetectors#mimeTypeOf(String, InputStream)}.
- * @return See {@link MimeTypeDetectors#mimeTypeOf(String, InputStream)}.
- * @throws IOException See {@link MimeTypeDetectors#mimeTypeOf(String,
InputStream)}.
- */
- public static String of( String name,
- InputStream content ) throws IOException {
- return DEFAULT_DETECTORS.mimeTypeOf(name, content);
- }
-
- /**
- * @param config See {@link
MimeTypeDetectors#removeDetector(MimeTypeDetectorConfig)}.
- * @return See {@link MimeTypeDetectors#removeDetector(MimeTypeDetectorConfig)}.
- * @see MimeTypeDetectors#removeDetector(MimeTypeDetectorConfig)
- */
- public static boolean removeDetector( MimeTypeDetectorConfig config ) {
- return DEFAULT_DETECTORS.removeDetector(config);
- }
-
- /**
- * @param classLoaderFactory See {@link
MimeTypeDetectors#setClassLoaderFactory(ClassLoaderFactory)}.
- * @see MimeTypeDetectors#setClassLoaderFactory(ClassLoaderFactory)
- */
- public static void setClassLoaderFactory( ClassLoaderFactory classLoaderFactory ) {
- DEFAULT_DETECTORS.setClassLoaderFactory(classLoaderFactory);
- }
-}
Modified:
trunk/dna-repository/src/main/java/org/jboss/dna/repository/mimetype/MimeTypeDetectorConfig.java
===================================================================
---
trunk/dna-repository/src/main/java/org/jboss/dna/repository/mimetype/MimeTypeDetectorConfig.java 2009-05-21
17:01:04 UTC (rev 919)
+++
trunk/dna-repository/src/main/java/org/jboss/dna/repository/mimetype/MimeTypeDetectorConfig.java 2009-05-21
19:03:27 UTC (rev 920)
@@ -25,6 +25,7 @@
import java.util.Map;
import org.jboss.dna.common.component.ComponentConfig;
+import org.jboss.dna.graph.mimetype.MimeTypeDetector;
/**
* @author jverhaeg
@@ -32,10 +33,23 @@
public class MimeTypeDetectorConfig extends ComponentConfig {
public MimeTypeDetectorConfig( String name,
- String description,
- Map<String, Object> properties,
- String classname,
- String[] classpath ) {
+ String description,
+ Map<String, Object> properties,
+ String classname,
+ String[] classpath ) {
super(name, description, properties, classname, classpath);
}
+
+ public MimeTypeDetectorConfig( String name,
+ String description,
+ Map<String, Object> properties,
+ Class<? extends MimeTypeDetector> clazz ) {
+ super(name, description, properties, clazz.getName());
+ }
+
+ public MimeTypeDetectorConfig( String name,
+ String description,
+ Class<? extends MimeTypeDetector> clazz ) {
+ super(name, description, clazz.getName());
+ }
}
Modified:
trunk/dna-repository/src/main/java/org/jboss/dna/repository/mimetype/MimeTypeDetectors.java
===================================================================
---
trunk/dna-repository/src/main/java/org/jboss/dna/repository/mimetype/MimeTypeDetectors.java 2009-05-21
17:01:04 UTC (rev 919)
+++
trunk/dna-repository/src/main/java/org/jboss/dna/repository/mimetype/MimeTypeDetectors.java 2009-05-21
19:03:27 UTC (rev 920)
@@ -41,8 +41,6 @@
@ThreadSafe
public final class MimeTypeDetectors implements MimeTypeDetector {
- public static final MimeTypeDetectors DEFAULT_DETECTORS = new MimeTypeDetectors();
-
/**
* Class loader factory instance that always returns the {@link
Thread#getContextClassLoader() current thread's context class
* loader}, or if <code>null</code> the class loader for this class.
Modified:
trunk/dna-repository/src/main/java/org/jboss/dna/repository/sequencer/StreamSequencerAdapter.java
===================================================================
---
trunk/dna-repository/src/main/java/org/jboss/dna/repository/sequencer/StreamSequencerAdapter.java 2009-05-21
17:01:04 UTC (rev 919)
+++
trunk/dna-repository/src/main/java/org/jboss/dna/repository/sequencer/StreamSequencerAdapter.java 2009-05-21
19:03:27 UTC (rev 920)
@@ -41,7 +41,6 @@
import org.jboss.dna.graph.sequencer.StreamSequencer;
import org.jboss.dna.graph.sequencer.StreamSequencerContext;
import org.jboss.dna.repository.RepositoryI18n;
-import org.jboss.dna.repository.mimetype.MimeType;
import org.jboss.dna.repository.observation.NodeChange;
import org.jboss.dna.repository.util.RepositoryNodePath;
@@ -234,21 +233,19 @@
assert sequencedProperty != null;
assert context != null;
assert problems != null;
- // Note: getMimeType() will still operate lazily, and thus throw a
SequencerException, since it is very intrusive and
- // potentially slow-running.
ValueFactories factories = context.getExecutionContext().getValueFactories();
- // TODO: Is this safe?
Path path = factories.getPathFactory().create(input.getLocation().getPath());
Set<org.jboss.dna.graph.property.Property> props = new
HashSet<org.jboss.dna.graph.property.Property>(
input.getPropertiesByName()
.values());
props = Collections.unmodifiableSet(props);
- String mimeType = getMimeType(sequencedProperty,
path.getLastSegment().getName().getLocalName());
+ String mimeType = getMimeType(context, sequencedProperty,
path.getLastSegment().getName().getLocalName());
return new StreamSequencerContext(context.getExecutionContext(), path, props,
mimeType, problems);
}
- protected String getMimeType( Property sequencedProperty,
+ protected String getMimeType( SequencerContext context,
+ Property sequencedProperty,
String name ) {
SequencerException err = null;
String mimeType = null;
@@ -256,7 +253,7 @@
try {
// Parallel the JCR lemma for converting objects into streams
stream = new ByteArrayInputStream(sequencedProperty.toString().getBytes());
- mimeType = MimeType.of(name, stream);
+ mimeType =
context.getExecutionContext().getMimeTypeDetector().mimeTypeOf(name, stream);
return mimeType;
} catch (Exception error) {
err = new SequencerException(error);
Modified: trunk/dna-repository/src/test/java/org/jboss/dna/repository/DnaEngineTest.java
===================================================================
---
trunk/dna-repository/src/test/java/org/jboss/dna/repository/DnaEngineTest.java 2009-05-21
17:01:04 UTC (rev 919)
+++
trunk/dna-repository/src/test/java/org/jboss/dna/repository/DnaEngineTest.java 2009-05-21
19:03:27 UTC (rev 920)
@@ -37,7 +37,6 @@
import org.jboss.dna.graph.connector.RepositoryConnection;
import org.jboss.dna.graph.connector.inmemory.InMemoryRepositorySource;
import org.jboss.dna.graph.mimetype.MimeTypeDetector;
-import org.jboss.dna.repository.mimetype.MimeTypeDetectors;
import org.jboss.dna.repository.observation.NodeChanges;
import org.jboss.dna.repository.sequencer.MockSequencerA;
import org.jboss.dna.repository.sequencer.SequencingService;
@@ -74,7 +73,7 @@
assertThat(engine.getRepositorySource("config repo"),
is(notNullValue()));
assertThat(engine.getRepositorySource("config repo"),
is(instanceOf(InMemoryRepositorySource.class)));
- RepositoryLibrary library =
engine.getRepositoryService().getRepositorySourceManager();
+ RepositoryLibrary library =
engine.getRepositoryService().getRepositoryLibrary();
assertThat(library.getConnectionPool("config repo").getInUseCount(),
is(0));
RepositoryConnection connection = library.getConnectionPool("config
repo").getConnection();
@@ -109,7 +108,7 @@
assertThat(engine.getRepositorySource("JCR"),
is(instanceOf(InMemoryRepositorySource.class)));
assertThat(engine.getRepositorySource("JCR").getName(),
is("JCR"));
- RepositoryLibrary library =
engine.getRepositoryService().getRepositorySourceManager();
+ RepositoryLibrary library =
engine.getRepositoryService().getRepositoryLibrary();
RepositoryConnection connection =
library.getConnectionPool("JCR").getConnection();
assertThat(connection.ping(500, TimeUnit.MILLISECONDS), is(true));
connection.close();
@@ -135,10 +134,8 @@
assertThat(engine.getRepositorySource("config repo"),
is(notNullValue()));
assertThat(engine.getRepositorySource("config repo"),
is(instanceOf(InMemoryRepositorySource.class)));
- RepositoryLibrary library =
engine.getRepositoryService().getRepositorySourceManager();
- MimeTypeDetectors detectors = library.getMimeTypeDetectors();
-
- assertThat(detectors.mimeTypeOf("test", new
ByteArrayInputStream("This is useless data".getBytes())),
is("mock"));
+ MimeTypeDetector detector = engine.getExecutionContext().getMimeTypeDetector();
+ assertThat(detector.mimeTypeOf("test", new
ByteArrayInputStream("This is useless data".getBytes())),
is("mock"));
}
@Test
Modified:
trunk/dna-repository/src/test/java/org/jboss/dna/repository/RepositoryServiceTest.java
===================================================================
---
trunk/dna-repository/src/test/java/org/jboss/dna/repository/RepositoryServiceTest.java 2009-05-21
17:01:04 UTC (rev 919)
+++
trunk/dna-repository/src/test/java/org/jboss/dna/repository/RepositoryServiceTest.java 2009-05-21
19:03:27 UTC (rev 920)
@@ -282,7 +282,7 @@
service.getAdministrator().start();
// Get the source, which should be configured ...
- RepositorySource repositorySourceA =
service.getRepositorySourceManager().getSource("source A");
+ RepositorySource repositorySourceA =
service.getRepositoryLibrary().getSource("source A");
assertThat(repositorySourceA, is(instanceOf(FakeRepositorySource.class)));
FakeRepositorySource sourceA = (FakeRepositorySource)repositorySourceA;
Modified:
trunk/dna-repository/src/test/java/org/jboss/dna/repository/mimetype/AbstractMimeTypeTest.java
===================================================================
---
trunk/dna-repository/src/test/java/org/jboss/dna/repository/mimetype/AbstractMimeTypeTest.java 2009-05-21
17:01:04 UTC (rev 919)
+++
trunk/dna-repository/src/test/java/org/jboss/dna/repository/mimetype/AbstractMimeTypeTest.java 2009-05-21
19:03:27 UTC (rev 920)
@@ -43,26 +43,29 @@
public abstract class AbstractMimeTypeTest {
private final MimeTypeDetectorConfig config;
+ private MimeTypeDetectors detectors;
protected AbstractMimeTypeTest( Class<? extends MimeTypeDetector> detector ) {
assertThat(detector, notNullValue());
- this.config = new MimeTypeDetectorConfig("MIME-Type Detector",
"MIME-Type Detector", Collections.<String, Object>emptyMap(),
detector.getName(), null);
+ this.config = new MimeTypeDetectorConfig("MIME-Type Detector",
"MIME-Type Detector",
+ Collections.<String,
Object>emptyMap(), detector.getName(), null);
}
@Before
public void before() throws Exception {
- MimeType.addDetector(config);
+ detectors = new MimeTypeDetectors();
+ detectors.addDetector(config);
}
@After
public void after() {
- MimeType.removeDetector(config);
+ detectors.removeDetector(config);
}
protected void testMimeType( String name,
String mimeType ) throws Exception {
InputStream content = new
File("../../dna-repository/src/test/resources/" +
name).toURI().toURL().openStream();
- assertThat(MimeType.of(name, content), is(mimeType));
+ assertThat(detectors.mimeTypeOf(name, content), is(mimeType));
}
protected abstract String expectedMimeTypeForText_test_txt();
Deleted:
trunk/dna-repository/src/test/java/org/jboss/dna/repository/mimetype/MimeTypeTest.java
===================================================================
---
trunk/dna-repository/src/test/java/org/jboss/dna/repository/mimetype/MimeTypeTest.java 2009-05-21
17:01:04 UTC (rev 919)
+++
trunk/dna-repository/src/test/java/org/jboss/dna/repository/mimetype/MimeTypeTest.java 2009-05-21
19:03:27 UTC (rev 920)
@@ -1,52 +0,0 @@
-/*
- * JBoss DNA (
http://www.jboss.org/dna)
- * See the COPYRIGHT.txt file distributed with this work for information
- * regarding copyright ownership. Some portions may be licensed
- * to Red Hat, Inc. under one or more contributor license agreements.
- * See the AUTHORS.txt file in the distribution for a full listing of
- * individual contributors.
- *
- * JBoss DNA is free software. Unless otherwise indicated, all code in JBoss DNA
- * is licensed to you under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * JBoss DNA is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
- */
-package org.jboss.dna.repository.mimetype;
-
-import static org.hamcrest.core.Is.is;
-import static org.junit.Assert.assertThat;
-import java.io.File;
-import java.io.InputStream;
-import org.junit.Test;
-
-/**
- * @author John Verhaeg
- */
-public class MimeTypeTest {
-
- private final void testMimeType( String name,
- String mimeType ) throws Exception {
- InputStream content = new File("src/test/resources/" +
name).toURI().toURL().openStream();
- assertThat(MimeType.of(name, content), is(mimeType));
- }
-
- @Test
- public void
shouldProvideDefaultTextMimeTypeWhenNoDetectorsRegisteredAndNoNullsInContent() throws
Exception {
- testMimeType("test.txt", "text/plain");
- }
-
- @Test
- public void
shouldProvideDefaultBinaryMimeTypeWhenNoDetectorsRegisteredAndNoNullsInContent() throws
Exception {
- testMimeType("docs/plain-text-utf16be.txt",
"application/octet-stream");
- }
-}
Modified:
trunk/dna-repository/src/test/java/org/jboss/dna/repository/sequencer/StreamSequencerAdapterTest.java
===================================================================
---
trunk/dna-repository/src/test/java/org/jboss/dna/repository/sequencer/StreamSequencerAdapterTest.java 2009-05-21
17:01:04 UTC (rev 919)
+++
trunk/dna-repository/src/test/java/org/jboss/dna/repository/sequencer/StreamSequencerAdapterTest.java 2009-05-21
19:03:27 UTC (rev 920)
@@ -50,6 +50,7 @@
import org.jboss.dna.graph.sequencer.SequencerOutput;
import org.jboss.dna.graph.sequencer.StreamSequencer;
import org.jboss.dna.graph.sequencer.StreamSequencerContext;
+import org.jboss.dna.repository.mimetype.MimeTypeDetectors;
import org.jboss.dna.repository.observation.NodeChange;
import org.jboss.dna.repository.util.RepositoryNodePath;
import org.junit.Before;
@@ -79,7 +80,8 @@
@Before
public void beforeEach() {
problems = new SimpleProblems();
- this.context = new ExecutionContext();
+ // Set up the MIME type detectors ...
+ this.context = new ExecutionContext().with(new MimeTypeDetectors());
this.sequencerOutput = new SequencerOutputMap(this.context.getValueFactories());
final SequencerOutputMap finalOutput = sequencerOutput;
@@ -277,8 +279,7 @@
try {
graph.getNodeAt("/d");
fail();
- }
- catch(PathNotFoundException pnfe) {
+ } catch (PathNotFoundException pnfe) {
// Expected
}
assertThat(nodeC, is(notNullValue()));
@@ -314,15 +315,13 @@
try {
graph.getNodeAt("/d");
fail();
- }
- catch(PathNotFoundException pnfe) {
+ } catch (PathNotFoundException pnfe) {
// Expected
}
try {
graph.getNodeAt("/x");
fail();
- }
- catch(PathNotFoundException pnfe) {
+ } catch (PathNotFoundException pnfe) {
// Expected
}
assertThat(nodeC, is(notNullValue()));
@@ -479,13 +478,15 @@
@Test
public void shouldProvideInputProperties() throws Exception {
-
this.sequencedProperty = mock(Property.class);
graph.create("/a").and().create("/a/b").and().create("/a/b/c");
graph.set("x").on("/a/b/c").to(true);
graph.set("y").on("/a/b/c").to(Arrays.asList(new String[]
{"asdf", "xyzzy"}));
Node input = graph.getNodeAt("/a/b/c");
- StreamSequencerContext sequencerContext =
sequencer.createStreamSequencerContext(input, sequencedProperty, seqContext, problems);
+ StreamSequencerContext sequencerContext =
sequencer.createStreamSequencerContext(input,
+
sequencedProperty,
+
seqContext,
+
problems);
assertThat(sequencerContext.getInputProperties(), notNullValue());
assertThat(sequencerContext.getInputProperties().isEmpty(), is(false));
assertThat(sequencerContext.getInputProperties().size(), is(3));