james-mime4j-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From b...@apache.org
Subject svn commit: r923003 - in /james/mime4j/trunk/dom/src: main/java/org/apache/james/mime4j/dom/ main/java/org/apache/james/mime4j/message/ main/resources/META-INF/services/ test/java/org/apache/james/mime4j/dom/
Date Sun, 14 Mar 2010 23:18:52 GMT
Author: bago
Date: Sun Mar 14 23:18:51 2010
New Revision: 923003

URL: http://svn.apache.org/viewvc?rev=923003&view=rev
Log:
Proposal for a MessageBuilderFactory/MessageBuilder way to create new Messages similar to
the well known DocumentBuilderFactory/DocumentBuilder pattern. (MIME4J-175)

Added:
    james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/dom/MessageBuilder.java 
 (with props)
    james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/dom/MessageBuilderFactory.java
  (with props)
    james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/dom/ServiceLoader.java  
(with props)
    james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/message/MessageBuilderFactoryImpl.java
  (with props)
    james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/message/MessageBuilderImpl.java
  (with props)
    james/mime4j/trunk/dom/src/main/resources/META-INF/services/
    james/mime4j/trunk/dom/src/main/resources/META-INF/services/org.apache.james.mime4j.dom.MessageBuilder
    james/mime4j/trunk/dom/src/test/java/org/apache/james/mime4j/dom/MessageBuilderFactoryTest.java
  (with props)

Added: james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/dom/MessageBuilder.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/dom/MessageBuilder.java?rev=923003&view=auto
==============================================================================
--- james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/dom/MessageBuilder.java (added)
+++ james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/dom/MessageBuilder.java Sun
Mar 14 23:18:51 2010
@@ -0,0 +1,35 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.james.mime4j.dom;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+import org.apache.james.mime4j.MimeException;
+
+public abstract class MessageBuilder {
+    
+    public abstract Message newMessage();
+
+    public abstract Message newMessage(Message source);
+
+    public abstract Message parse(InputStream source) throws MimeException, IOException;
+
+}

Propchange: james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/dom/MessageBuilder.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/dom/MessageBuilderFactory.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/dom/MessageBuilderFactory.java?rev=923003&view=auto
==============================================================================
--- james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/dom/MessageBuilderFactory.java
(added)
+++ james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/dom/MessageBuilderFactory.java
Sun Mar 14 23:18:51 2010
@@ -0,0 +1,39 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.james.mime4j.dom;
+
+import org.apache.james.mime4j.MimeException;
+
+/**
+ * A MessageBuilderFactory is used to create EntityBuilder instances.
+ * 
+ * MessageBuilderFactory.newInstance() is used to get access to an implementation
+ * of MessageBuilderFactory.
+ * Then the method newMessageBuilder is used to create a new EntityBuilder object.
+ */
+public abstract class MessageBuilderFactory {
+
+    public abstract MessageBuilder newMessageBuilder() throws MimeException;
+    
+    public static MessageBuilderFactory newInstance() throws MimeException {
+        return ServiceLoader.load(MessageBuilderFactory.class);
+    }
+    
+}

Propchange: james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/dom/MessageBuilderFactory.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/dom/ServiceLoader.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/dom/ServiceLoader.java?rev=923003&view=auto
==============================================================================
--- james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/dom/ServiceLoader.java (added)
+++ james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/dom/ServiceLoader.java Sun
Mar 14 23:18:51 2010
@@ -0,0 +1,95 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+package org.apache.james.mime4j.dom;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.net.URL;
+import java.util.Enumeration;
+
+/**
+ * Utility class to load Service Providers (SPI).
+ * This will deprecated as soon as mime4j will be upgraded to Java6
+ * as Java6 has javax.util.ServiceLoader as a core class.
+ */
+class ServiceLoader {
+
+    private ServiceLoader() {
+    }
+
+    /**
+     * Loads a Service Provider for the given interface/class (SPI).
+     */
+    static <T> T load(Class<T> spiClass) {
+        String spiResURI = "META-INF/services/" + spiClass.getName();
+        ClassLoader classLoader = spiClass.getClassLoader();
+        Enumeration<URL> resources;
+        try {
+            resources = classLoader.getResources(spiResURI);
+        } catch (IOException e) {
+            return null;
+        }
+
+        while (resources.hasMoreElements()) {
+            URL resource = resources.nextElement();
+            BufferedReader reader = null;
+            try {
+                reader = new BufferedReader(new InputStreamReader(resource
+                        .openStream()));
+                String line;
+                while ((line = reader.readLine()) != null) {
+                    line = line.trim();
+                    int cmtIdx = line.indexOf('#');
+                    if (cmtIdx != -1) {
+                        line = line.substring(0, cmtIdx);
+                        line = line.trim();
+                    }
+
+                    if (line.length() == 0) {
+                        continue;
+                    }
+
+                    Class<?> implClass;
+                    try {
+                        implClass = classLoader.loadClass(line);
+                        
+                        if (spiClass.isAssignableFrom(implClass)) {
+                            Object impl = implClass.newInstance();
+                            return spiClass.cast(impl);
+                        }
+                    } catch (Exception e) {
+                        e.printStackTrace();
+                    }
+                }
+            } catch (IOException e) {
+                e.printStackTrace();
+            } finally {
+                if (reader != null) {
+                    try {
+                        reader.close();
+                    } catch (IOException e) {
+                    }
+                }
+            }
+        }
+
+        return null;
+    }
+}
\ No newline at end of file

Propchange: james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/dom/ServiceLoader.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/message/MessageBuilderFactoryImpl.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/message/MessageBuilderFactoryImpl.java?rev=923003&view=auto
==============================================================================
--- james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/message/MessageBuilderFactoryImpl.java
(added)
+++ james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/message/MessageBuilderFactoryImpl.java
Sun Mar 14 23:18:51 2010
@@ -0,0 +1,32 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+package org.apache.james.mime4j.message;
+
+import org.apache.james.mime4j.MimeException;
+import org.apache.james.mime4j.dom.MessageBuilder;
+import org.apache.james.mime4j.dom.MessageBuilderFactory;
+
+public class MessageBuilderFactoryImpl extends MessageBuilderFactory {
+
+    @Override
+    public MessageBuilder newMessageBuilder() throws MimeException {
+        return new MessageBuilderImpl();
+    }
+
+}

Propchange: james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/message/MessageBuilderFactoryImpl.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/message/MessageBuilderImpl.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/message/MessageBuilderImpl.java?rev=923003&view=auto
==============================================================================
--- james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/message/MessageBuilderImpl.java
(added)
+++ james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/message/MessageBuilderImpl.java
Sun Mar 14 23:18:51 2010
@@ -0,0 +1,27 @@
+package org.apache.james.mime4j.message;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+import org.apache.james.mime4j.MimeException;
+import org.apache.james.mime4j.dom.Message;
+import org.apache.james.mime4j.dom.MessageBuilder;
+
+public class MessageBuilderImpl extends MessageBuilder {
+
+    @Override
+    public Message newMessage() {
+        return new MessageImpl();
+    }
+
+    @Override
+    public Message newMessage(Message source) {
+        return new MessageImpl(source);
+    }
+
+    @Override
+    public Message parse(InputStream source) throws MimeException, IOException {
+        return new MessageImpl(source);
+    }
+
+}

Propchange: james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/message/MessageBuilderImpl.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: james/mime4j/trunk/dom/src/main/resources/META-INF/services/org.apache.james.mime4j.dom.MessageBuilder
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/dom/src/main/resources/META-INF/services/org.apache.james.mime4j.dom.MessageBuilder?rev=923003&view=auto
==============================================================================
--- james/mime4j/trunk/dom/src/main/resources/META-INF/services/org.apache.james.mime4j.dom.MessageBuilder
(added)
+++ james/mime4j/trunk/dom/src/main/resources/META-INF/services/org.apache.james.mime4j.dom.MessageBuilder
Sun Mar 14 23:18:51 2010
@@ -0,0 +1 @@
+org.apache.james.mime4j.message.MessageBuilderImpl
\ No newline at end of file

Added: james/mime4j/trunk/dom/src/test/java/org/apache/james/mime4j/dom/MessageBuilderFactoryTest.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/dom/src/test/java/org/apache/james/mime4j/dom/MessageBuilderFactoryTest.java?rev=923003&view=auto
==============================================================================
--- james/mime4j/trunk/dom/src/test/java/org/apache/james/mime4j/dom/MessageBuilderFactoryTest.java
(added)
+++ james/mime4j/trunk/dom/src/test/java/org/apache/james/mime4j/dom/MessageBuilderFactoryTest.java
Sun Mar 14 23:18:51 2010
@@ -0,0 +1,41 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+package org.apache.james.mime4j.dom;
+
+import org.apache.james.mime4j.MimeException;
+
+import junit.framework.TestCase;
+
+public class MessageBuilderFactoryTest extends TestCase {
+
+    public void testNewInstance() throws MimeException {
+        MessageBuilderFactory factory = MessageBuilderFactory.newInstance();
+        assertNotNull(factory);
+    }
+
+
+    public void testNewMessageBuilder() throws MimeException {
+        MessageBuilderFactory factory = MessageBuilderFactory.newInstance();
+        assertNotNull(factory);
+        MessageBuilder builder = factory.newMessageBuilder();
+        Message message = builder.newMessage();
+        assertNotNull(message);
+    }
+
+}

Propchange: james/mime4j/trunk/dom/src/test/java/org/apache/james/mime4j/dom/MessageBuilderFactoryTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain



Mime
View raw message