1 /*
2 * ModeShape (http://www.modeshape.org)
3 * See the COPYRIGHT.txt file distributed with this work for information
4 * regarding copyright ownership. Some portions may be licensed
5 * to Red Hat, Inc. under one or more contributor license agreements.
6 * See the AUTHORS.txt file in the distribution for a full listing of
7 * individual contributors.
8 *
9 * ModeShape is free software. Unless otherwise indicated, all code in ModeShape
10 * is licensed to you under the terms of the GNU Lesser General Public License as
11 * published by the Free Software Foundation; either version 2.1 of
12 * the License, or (at your option) any later version.
13 *
14 * ModeShape is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this software; if not, write to the Free
21 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
23 */
24 package org.modeshape.graph.mimetype;
25
26 import java.io.IOException;
27 import java.io.InputStream;
28 import net.jcip.annotations.ThreadSafe;
29 import org.modeshape.common.component.ClassLoaderFactory;
30 import org.modeshape.common.component.ComponentLibrary;
31 import org.modeshape.common.component.StandardClassLoaderFactory;
32 import org.modeshape.common.util.Logger;
33
34 /**
35 * Facility for managing {@link MimeTypeDetectorConfig}s.
36 */
37 @ThreadSafe
38 public final class MimeTypeDetectors implements MimeTypeDetector {
39
40 /**
41 * Class loader factory instance that always returns the {@link Thread#getContextClassLoader() current thread's context class
42 * loader}, or if <code>null</code> the class loader for this class.
43 */
44 protected static final ClassLoaderFactory DEFAULT_CLASSLOADER_FACTORY = new StandardClassLoaderFactory(
45 MimeTypeDetectors.class.getClassLoader());
46
47 private final ComponentLibrary<MimeTypeDetector, MimeTypeDetectorConfig> library;
48 private Logger logger;
49
50 public MimeTypeDetectors() {
51 library = new ComponentLibrary<MimeTypeDetector, MimeTypeDetectorConfig>(true);
52 library.setClassLoaderFactory(DEFAULT_CLASSLOADER_FACTORY);
53 }
54
55 /**
56 * Adds the configuration for a MIME-type detector <em>before</em> any previously added configurations, or updates any
57 * existing one that represents the {@link MimeTypeDetectorConfig#equals(Object) same configuration}
58 *
59 * @param config the new configuration; must not be <code>null</code>.
60 * @return <code>true</code> if the detector was added, or <code>false</code> if there already was an existing detector
61 * configuration.
62 * @see #removeDetector(MimeTypeDetectorConfig)
63 */
64 public boolean addDetector( MimeTypeDetectorConfig config ) {
65 return library.add(config);
66 }
67
68 /**
69 * Gets the class loader factory that should be used to load MIME-type detectors. By default, this service uses a factory that
70 * will return either the {@link Thread#getContextClassLoader() current thread's context class loader}, or if
71 * <code>null</code> the class loader for this class.
72 *
73 * @return the class loader factory; never <code>null</code>
74 * @see #setClassLoaderFactory(ClassLoaderFactory)
75 */
76 public ClassLoaderFactory getClassLoaderFactory() {
77 return library.getClassLoaderFactory();
78 }
79
80 /**
81 * Gets the logger for this system
82 *
83 * @return the logger
84 */
85 public synchronized Logger getLogger() {
86 if (logger == null) {
87 logger = Logger.getLogger(getClass());
88 }
89 return logger;
90 }
91
92 /**
93 * Returns the first non-null result of iterating over the {@link #addDetector(MimeTypeDetectorConfig) registered} MIME-type
94 * detectors in the reverse order in which they were registered to determine the MIME-type of a data source, using its
95 * supplied content and/or its supplied name, depending upon the implementation. If the MIME-type cannot be determined by any
96 * registered detector, "text/plain" or "application/octet-stream" will be returned, the former only if it is determined the
97 * stream contains no nulls.
98 *
99 * @param name The name of the data source; may be <code>null</code>.
100 * @param content The content of the data source; may be <code>null</code>.
101 * @return The MIME-type of the data source; never <code>null</code>.
102 * @throws IOException If an error occurs reading the supplied content.
103 */
104 public String mimeTypeOf( String name,
105 InputStream content ) throws IOException {
106 if (content != null && content.markSupported()) {
107 content.mark(Integer.MAX_VALUE);
108 }
109 // Check if registered detectors can determine MIME-type
110 for (MimeTypeDetector detector : library.getInstances()) {
111 String mimeType = detector.mimeTypeOf(name, content);
112 if (mimeType != null) return mimeType;
113 }
114 // If not, try to analyze stream to determine if it represents text or binary content
115 if (content != null && content.markSupported()) {
116 try {
117 content.reset();
118 for (int chr = content.read(); chr >= 0; chr = content.read()) {
119 if (chr == 0) return "application/octet-stream";
120 }
121 } catch (IOException meansTooManyBytesRead) {
122 return "application/octet-stream";
123 }
124 }
125 return "text/plain";
126 }
127
128 /**
129 * Removes the configuration for a MIME-type detector.
130 *
131 * @param config the configuration to be removed; must not be <code>null</code>.
132 * @return <code>true</code> if the configuration was removed, or <code>false</code> if there was no existing configuration
133 * @see #addDetector(MimeTypeDetectorConfig)
134 */
135 public boolean removeDetector( MimeTypeDetectorConfig config ) {
136 return library.remove(config);
137 }
138
139 /**
140 * Sets the Maven Repository that should be used to load the MIME-type detectors. By default, this service uses a factory that
141 * will return either the {@link Thread#getContextClassLoader() current thread's context class loader}, or if
142 * <code>null</code> the class loader for this class.
143 *
144 * @param classLoaderFactory the class loader factory, or <code>null</code> if the default class loader factory should be
145 * used.
146 * @see #getClassLoaderFactory()
147 */
148 public void setClassLoaderFactory( ClassLoaderFactory classLoaderFactory ) {
149 library.setClassLoaderFactory(classLoaderFactory != null ? classLoaderFactory : DEFAULT_CLASSLOADER_FACTORY);
150 }
151
152 /**
153 * Sets the logger for this system.
154 *
155 * @param logger the logger, or <code>null</code> if the standard logging should be used
156 */
157 public synchronized void setLogger( Logger logger ) {
158 this.logger = logger != null ? logger : getLogger();
159 }
160 }