You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@activemq.apache.org by ra...@apache.org on 2007/05/02 09:55:53 UTC

svn commit: r534340 - in /activemq/camel/trunk/camel-core/src/main/java/org/apache/camel: ./ component/file/ impl/ util/

Author: rajdavies
Date: Wed May  2 00:55:52 2007
New Revision: 534340

URL: http://svn.apache.org/viewvc?view=rev&rev=534340
Log:
Added an id to the Message

Added:
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/UIdGenerator.java   (with props)
Modified:
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/Message.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileComponent.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileEndpoint.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileProducer.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultExchange.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/MessageSupport.java

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/Message.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/Message.java?view=diff&rev=534340&r1=534339&r2=534340
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/Message.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/Message.java Wed May  2 00:55:52 2007
@@ -28,6 +28,17 @@
 public interface Message {
     
     /**
+     * @return the id of the message
+     */
+    String getMessageId();
+    
+    /**
+     * set the id of the message
+     * @param messageId
+     */
+    void setMessageId(String messageId);
+    
+    /**
      * Accesses a specific header
      *
      * @param name
@@ -59,6 +70,12 @@
      * @return all the headers in a Map
      */
     Map<String, Object> getHeaders();
+    
+    /**
+     * Set all the headers associated with this message
+     * @param headers
+     */
+    void setHeaders(Map<String,Object> headers);
 
     /**
      * Returns the body of the message as a POJO

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileComponent.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileComponent.java?view=diff&rev=534340&r1=534339&r2=534340
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileComponent.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileComponent.java Wed May  2 00:55:52 2007
@@ -20,6 +20,7 @@
 import org.apache.camel.CamelContext;
 import org.apache.camel.Endpoint;
 import org.apache.camel.impl.DefaultComponent;
+import org.apache.camel.util.IntrospectionSupport;
 
 import java.io.File;
 import java.util.Map;
@@ -40,6 +41,7 @@
     protected Endpoint<FileExchange> createEndpoint(String uri, String remaining, Map parameters) throws Exception {
         File file = new File(remaining);
         FileEndpoint result = new FileEndpoint(file, remaining, this);
+        IntrospectionSupport.setProperties(result, parameters);
         return result;
     }
 }

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileEndpoint.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileEndpoint.java?view=diff&rev=534340&r1=534339&r2=534340
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileEndpoint.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileEndpoint.java Wed May  2 00:55:52 2007
@@ -31,6 +31,7 @@
  */
 public class FileEndpoint extends PollingEndpoint<FileExchange> {
     private File file;
+    private boolean autoCreate=true;
 
     protected FileEndpoint(File file, String endpointUri, FileComponent component) {
         super(endpointUri, component);
@@ -73,14 +74,33 @@
      * @see org.apache.camel.Endpoint#createExchange()
      */
     public FileExchange createExchange() {
-        return createExchange(this.file);
+        return createExchange(getFile());
     }
 
     public File getFile() {
+        if (autoCreate && !file.exists()) {
+            file.mkdirs();
+        }
         return file;
     }
 
     public boolean isSingleton() {
         return true;
+    }
+
+    
+    /**
+     * @return the autoCreate
+     */
+    public boolean isAutoCreate(){
+        return this.autoCreate;
+    }
+
+    
+    /**
+     * @param autoCreate the autoCreate to set
+     */
+    public void setAutoCreate(boolean autoCreate){
+        this.autoCreate=autoCreate;
     }
 }

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileProducer.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileProducer.java?view=diff&rev=534340&r1=534339&r2=534340
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileProducer.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileProducer.java Wed May  2 00:55:52 2007
@@ -46,25 +46,36 @@
         process(endpoint.toExchangeType(exchange));
     }
 
-    public void process(FileExchange exchange) {
-        ByteBuffer payload = exchange.getIn().getBody(ByteBuffer.class);
+    public void process(FileExchange exchange){
+        String fileName = exchange.getIn().getMessageId();
+        ByteBuffer payload=exchange.getIn().getBody(ByteBuffer.class);
         payload.flip();
         File file = null;
-
-        if (endpoint.getFile() != null && endpoint.getFile().isDirectory()) {
-            file = new File(endpoint.getFile(), exchange.getFile().getName());
+        if(endpoint.getFile()!=null&&endpoint.getFile().isDirectory()){
+            
+            file=new File(endpoint.getFile(),fileName);
+           
+        }else{
+            file=new File(fileName);
         }
-        else {
-            file = exchange.getFile();
-        }
-        try {
-            FileChannel fc = new RandomAccessFile(file, "rw").getChannel();
+        buildDirectory(file);
+        try{
+            FileChannel fc=new RandomAccessFile(file,"rw").getChannel();
             fc.position(fc.size());
             fc.write(payload);
             fc.close();
+        }catch(Throwable e){
+            log.error("Failed to write to File: "+file,e);
         }
-        catch (Throwable e) {
-            log.error("Failed to write to File: " + file, e);
+    }
+    
+    private void buildDirectory(File file) {
+        String dirName = file.getAbsolutePath();
+        int index = dirName.lastIndexOf(File.separatorChar);
+        if (index > 0) {
+            dirName = dirName.substring(0,index);
+            File dir = new File(dirName);
+            dir.mkdirs();
         }
     }
 }

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultExchange.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultExchange.java?view=diff&rev=534340&r1=534339&r2=534340
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultExchange.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultExchange.java Wed May  2 00:55:52 2007
@@ -19,6 +19,7 @@
 import org.apache.camel.CamelContext;
 import org.apache.camel.Exchange;
 import org.apache.camel.Message;
+import org.apache.camel.util.UIdGenerator;
 
 import java.util.HashMap;
 import java.util.Map;
@@ -29,13 +30,14 @@
  * @version $Revision$
  */
 public class DefaultExchange implements Exchange {
+    private static final UIdGenerator defaultIdGenerator = new UIdGenerator();
     protected final CamelContext context;
     private Map<String, Object> headers;
     private Message in;
     private Message out;
     private Message fault;
     private Throwable exception;
-    private String exchangeId;
+    private String exchangeId = DefaultExchange.defaultIdGenerator.generateId();
 
     public DefaultExchange(CamelContext context) {
         this.context = context;

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/MessageSupport.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/MessageSupport.java?view=diff&rev=534340&r1=534339&r2=534340
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/MessageSupport.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/MessageSupport.java Wed May  2 00:55:52 2007
@@ -19,6 +19,7 @@
 
 import org.apache.camel.Exchange;
 import org.apache.camel.Message;
+import org.apache.camel.util.UIdGenerator;
 
 /**
  * A base class for implementation inheritence providing the core {@link Message} body
@@ -30,8 +31,11 @@
  * @version $Revision$
  */
 public abstract class MessageSupport implements Message {
+    private static final UIdGenerator defaultIdGenerator = new UIdGenerator();
     private Exchange exchange;
     private Object body;
+    private String messageId = defaultIdGenerator.generateId();
+    
 
     public Object getBody() {
         if (body == null) {
@@ -66,6 +70,7 @@
 
     public Message copy() {
         Message answer = newInstance();
+        answer.setMessageId(getMessageId());
         answer.setBody(getBody());
         answer.getHeaders().putAll(getHeaders());
         return answer;
@@ -94,5 +99,21 @@
      */
     protected Object createBody() {
         return null;
+    }
+
+    
+    /**
+     * @return the messageId
+     */
+    public String getMessageId(){
+        return this.messageId;
+    }
+
+    
+    /**
+     * @param messageId the messageId to set
+     */
+    public void setMessageId(String messageId){
+        this.messageId=messageId;
     }
 }

Added: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/UIdGenerator.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/UIdGenerator.java?view=auto&rev=534340
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/UIdGenerator.java (added)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/UIdGenerator.java Wed May  2 00:55:52 2007
@@ -0,0 +1,112 @@
+/**
+ *
+ * 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.camel.util;
+import java.net.InetAddress;
+import java.net.ServerSocket;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+/**
+ * Generator for Globally unique Strings.
+ */
+
+public class UIdGenerator{
+
+    private static final Logger log = Logger.getLogger(UIdGenerator.class.getName());
+    private static final String UNIQUE_STUB;
+    private static int instanceCount;
+    private static String hostName;
+    private String seed;
+    private long sequence;
+    
+    static {
+        String stub = "";
+        boolean canAccessSystemProps = true;
+        try{
+            SecurityManager sm = System.getSecurityManager();
+            if(sm != null){
+                sm.checkPropertiesAccess();
+            }
+        }catch(SecurityException se){
+            canAccessSystemProps = false;
+        }
+        
+        if ( canAccessSystemProps) {
+            try {
+                hostName = InetAddress.getLocalHost().getHostName();
+                ServerSocket ss = new ServerSocket(0);
+                stub="-" + ss.getLocalPort() + "-" + System.currentTimeMillis() + "-";
+                Thread.sleep(100);
+                ss.close();
+            }catch(Exception ioe){
+                log.log(Level.WARNING, "could not generate unique stub",ioe);
+            }
+        }else{
+            hostName="localhost";
+            stub = "-1-" +System.currentTimeMillis() +"-";
+        }
+        UNIQUE_STUB = stub;
+    }
+    
+    /**
+     * As we have to find the hostname as a side-affect of generating
+     * a unique stub, we allow it's easy retrevial here
+     * @return the local host name
+     */
+    
+    public static String getHostName(){
+        return hostName;
+    }
+    
+    /**
+     * Construct an IdGenerator
+     *
+     */
+    
+    public UIdGenerator(String prefix){
+        synchronized(UNIQUE_STUB){
+            this.seed = prefix + UNIQUE_STUB +(instanceCount++) +":";
+        }
+    }
+    
+    public UIdGenerator(){
+        this("ID:" + hostName);
+    }
+    
+    /**
+     * Generate a unqiue id
+     * @return a unique id
+     */
+    
+    public synchronized String generateId(){
+        return this.seed + (this.sequence++);
+    }
+    
+    /**
+     * Generate a unique ID - that is friendly for a URL or file system
+     * @return a unique id
+     */
+    public String generateSanitizedId(){
+        String result = generateId();
+        result = result.replace(':', '-');
+        result = result.replace('_', '-');
+        result = result.replace('.', '-');
+        return result;
+    }
+
+}

Propchange: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/UIdGenerator.java
------------------------------------------------------------------------------
    svn:eol-style = native