KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > transformers > encryption > AbstractEncryptionTransformer


1 /*
2  * $Id: AbstractEncryptionTransformer.java 3798 2006-11-04 04:07:14Z aperepel $
3  * --------------------------------------------------------------------------------------
4  * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
5  *
6  * The software in this package is published under the terms of the MuleSource MPL
7  * license, a copy of which has been included with this distribution in the
8  * LICENSE.txt file.
9  */

10
11 package org.mule.transformers.encryption;
12
13 import org.mule.MuleManager;
14 import org.mule.config.i18n.Message;
15 import org.mule.config.i18n.Messages;
16 import org.mule.transformers.AbstractTransformer;
17 import org.mule.umo.UMOEncryptionStrategy;
18 import org.mule.umo.lifecycle.InitialisationException;
19 import org.mule.umo.security.CryptoFailureException;
20 import org.mule.umo.transformer.TransformerException;
21
22 import java.io.UnsupportedEncodingException JavaDoc;
23
24 /**
25  * <code>EncryptionTransformer</code> will transform an array of bytes or string
26  * into an encrypted array of bytes
27  *
28  * @author <a HREF="mailto:ross.mason@symphonysoft.com">Ross Mason</a>
29  * @version $Revision: 3798 $
30  */

31 public abstract class AbstractEncryptionTransformer extends AbstractTransformer
32 {
33     private UMOEncryptionStrategy strategy = null;
34     private String JavaDoc strategyName = null;
35
36     public AbstractEncryptionTransformer()
37     {
38         registerSourceType(byte[].class);
39         registerSourceType(String JavaDoc.class);
40         setReturnClass(byte[].class);
41     }
42
43     public Object JavaDoc doTransform(Object JavaDoc src, String JavaDoc encoding) throws TransformerException
44     {
45         byte[] buf;
46         if (src instanceof String JavaDoc)
47         {
48             buf = src.toString().getBytes();
49         }
50         else
51         {
52             buf = (byte[])src;
53         }
54         try
55         {
56             byte[] result = getTransformedBytes(buf);
57             if (getReturnClass().equals(String JavaDoc.class))
58             {
59                 if (encoding != null)
60                 {
61                     try
62                     {
63                         return new String JavaDoc(result, encoding);
64                     }
65                     catch (UnsupportedEncodingException JavaDoc ex)
66                     {
67                         return new String JavaDoc(result);
68                     }
69                 }
70                 else
71                 {
72                     return new String JavaDoc(result);
73                 }
74             }
75             else
76             {
77                 return result;
78             }
79         }
80         catch (CryptoFailureException e)
81         {
82             throw new TransformerException(this, e);
83         }
84     }
85
86     protected abstract byte[] getTransformedBytes(byte[] buffer) throws CryptoFailureException;
87
88     /**
89      * Template method were deriving classes can do any initialisation after the
90      * properties have been set on this transformer
91      *
92      * @throws org.mule.umo.lifecycle.InitialisationException
93      */

94     public void initialise() throws InitialisationException
95     {
96         if (strategyName != null)
97         {
98             if (MuleManager.getInstance().getSecurityManager() == null)
99             {
100                 if (strategy == null)
101                 {
102                     throw new InitialisationException(new Message(Messages.AUTH_SECURITY_MANAGER_NOT_SET),
103                         this);
104
105                 }
106             }
107             else
108             {
109                 strategy = MuleManager.getInstance().getSecurityManager().getEncryptionStrategy(strategyName);
110             }
111         }
112         if (strategy == null)
113         {
114             throw new InitialisationException(new Message(Messages.ENCRYPT_STRATEGY_NOT_SET), this);
115         }
116     }
117
118     public UMOEncryptionStrategy getStrategy()
119     {
120         return strategy;
121     }
122
123     public void setStrategy(UMOEncryptionStrategy strategy)
124     {
125         this.strategy = strategy;
126     }
127
128     public String JavaDoc getStrategyName()
129     {
130         return strategyName;
131     }
132
133     public void setStrategyName(String JavaDoc strategyName)
134     {
135         this.strategyName = strategyName;
136     }
137 }
138
Popular Tags