summaryrefslogtreecommitdiffstats
path: root/site/trunk/site-publish/workingwithcolumnconverters.html
blob: 77144bacbb7926726d86787484c6d31c25953fb5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
<!--

    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.
-->


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML>
    <HEAD>
		<!-- generateKeywords macro -->
	    <META name="description" content="Apache Tuscany">
        <META name="keywords" content="apache, apache tuscany, tuscany, service, services, fabric, soa, service oriented architecture,  sca, service component architecture, das, sdo, csa, ruby, opensource">
		<!-- generateKeywords macro end -->
		
        <LINK type="text/css" rel="stylesheet" href="http://tuscany.apache.org/stylesheets/default.css">
        <LINK rel="SHORTCUT ICON" href="https://cwiki.apache.org/confluence/display/TUSCANY/$images/favicon.ico">   
        <TITLE>Apache Tuscany : WorkingWithColumnConverters</TITLE>
    <META http-equiv="Content-Type" content="text/html;charset=UTF-8"></HEAD>
    
    <BODY onload="init()">
        <!-- topNav macro -->
		<TABLE valign="top" border="0" cellspacing="0" cellpadding="0" width="100%" background="http://tuscany.apache.org/images/TuscanyLogoNEW_Text_120px_bg.jpg">
			<TR>
				<TD valing="top" align="left">
					<A href="https://cwiki.apache.org/confluence/pages/viewpage.action?spaceKey=TUSCANY&title=$siteroot"><IMG src="http://tuscany.apache.org/images/TuscanyLogoNEW_Text_120px_bg.jpg" height="91" width="25" border="0"></A>
				</TD>
				<TD>
					<A href="http://tuscany.apache.org/"><IMG src="http://tuscany.apache.org/images/TuscanyLogo.jpg" border="0"></A>
				</TD>
				
				<TD width="100%">
					&nbsp;
				</TD>

				<!-- Adds the edit page link to the top banner-->
				<TD valign="bottom">
					<DIV style="padding: 2px 10px; margin: 0px;">
						<A href="https://cwiki.apache.org/confluence/pages/editpage.action?pageId=52836">
						<IMG src="http://tuscany.apache.org/images/notep_16.gif" height="16" width="16" border="0" align="absmiddle" title="Edit Page"></A>
					</DIV>
				</TD>

			</TR>
		</TABLE>
        <!-- topNav macro end -->

		<!-- breadCrumbs macro -->
		<TABLE border="0" cellpadding="2" cellspacing="0" width="100%">
			<TR class="topBar">
				<TD align="left" valign="middle" class="topBarDiv" nowrap="true" width="100%">
					&nbsp;<A href="home.html" title="Apache Tuscany">Apache Tuscany</A>&nbsp;&gt;&nbsp;<A href="home.html" title="Home">Home</A>&nbsp;&gt;&nbsp;<A href="das-overview.html" title="DAS Overview">DAS Overview</A>&nbsp;&gt;&nbsp;<A href="das-java.html" title="DAS Java">DAS Java</A>&nbsp;&gt;&nbsp;<A href="das-java-documentation-menu.html" title="DAS Java Documentation Menu">DAS Java Documentation Menu</A>&nbsp;&gt;&nbsp;<A href="das-java-developer-guide.html" title="DAS Java Developer Guide">DAS Java Developer Guide</A>&nbsp;&gt;&nbsp;<A href="rdb-das-java.html" title="RDB DAS Java">RDB DAS Java</A>&nbsp;&gt;&nbsp;<A href="rdb-das-user-guide.html" title="RDB DAS - User Guide">RDB DAS - User Guide</A>&nbsp;&gt;&nbsp;<A href="" title="WorkingWithColumnConverters">WorkingWithColumnConverters</A>
				</TD>
				
				<TD align="right" valign="middle" class="topBarDiv" align="left" nowrap="true">
				<A href="http://mail-archives.apache.org/mod_mbox/tuscany-user">User List</A> | <A href="http://mail-archives.apache.org/mod_mbox/tuscany-dev">Dev List</A> | <A href="http://issues.apache.org/jira/browse/Tuscany">Issue Tracker</A>&nbsp;&nbsp;
				</TD>
			</TR>
		</TABLE>
		<!-- breadCrumbs macro end -->


        <TABLE border="0" cellpadding="0" width="100%" bgcolor="#FFFFFF">
            <TR>
                <TD align="left" valign="top">

					<!-- pageContent macro -->
					<DIV id="PageContent">
												
						<DIV class="pagecontent">
							<DIV class="wiki-content">
								<H1><A name="WorkingWithColumnConverters-WorkingWithColumnConverters"></A>WorkingWithColumnConverters</H1>

<P>Sometimes it is desireable to modify a column value retreived from a database column <B>before</B> it is stored into a DataObject property. One example might be a database column with possible values '0' or '1' but the desired DataObject property is 'false' or 'true'. Another example might be a String property that should be obfuscated or encryted before it is stored into a column.</P>

<P>The RDB DAS provides a simple &quot;converter&quot; framework that can be used for these cases and others. To use this framework the application developer must provide a new Converter class to provide the desired transformations from column to property and from property back to column. The provided converter must implement the following interface:</P>
<DIV class="code panel" style="border-width: 1px;"><DIV class="codeContent panelContent">
<PRE class="code-java">
<SPAN class="code-keyword">package</SPAN> org.apache.tuscany.das.rdb;

/**
 * A lightweight Table-column &lt;--&gt; DataObject-property converter framework. Converters allow a user to
 * insert a transformation between a column value and is destination DataObject property value. For example,
 * by <SPAN class="code-keyword">default</SPAN>, a VARCHAR column will be represented as a     <SPAN class="code-object">String</SPAN> in its corresponding DataObject property.
 * A user could insert a converter that transforms the the VARCHAR value to an <SPAN class="code-object">Integer</SPAN>. If <SPAN class="code-keyword">this</SPAN> is done then
 * although the column returns character data, the DataObject property will be an <SPAN class="code-object">Integer</SPAN>
 */
<SPAN class="code-keyword">public</SPAN> <SPAN class="code-keyword">interface</SPAN> Converter {

    /**
     * Transform the columnData object to a <SPAN class="code-keyword">new</SPAN> value and possibly <SPAN class="code-keyword">new</SPAN> type. This should be the inverse
     * operation of #getColumnValue
     *
     * @param columnData
     *            The column value to transorm
     * @<SPAN class="code-keyword">return</SPAN> Returns the transformed value
     */
    <SPAN class="code-object">Object</SPAN> getPropertyValue(<SPAN class="code-object">Object</SPAN> columnData);

    /**
     * Transform the columnData object to a <SPAN class="code-keyword">new</SPAN> value and possibly <SPAN class="code-keyword">new</SPAN> type. This should be the inverse
     * operation of #getPropertyValue
     *
     * @param propertyData
     *            The property value to transform
     * @<SPAN class="code-keyword">return</SPAN> Returns the transformed value
     */
    <SPAN class="code-object">Object</SPAN> getColumnValue(<SPAN class="code-object">Object</SPAN> propertyData);

}
</PRE>
</DIV></DIV>
<P>The following example illustrates the use of the converter framework to obfuscate a String value before it is stored to the database and deobfuscate it on read.</P>
<DIV class="code panel" style="border-width: 1px;"><DIV class="codeContent panelContent">
<PRE class="code-java">
DAS das = DAS.FACTORY.createDAS(getConfig(<SPAN class="code-quote">&quot;CustomerConfig.xml&quot;</SPAN>), getConnection());

        <SPAN class="code-comment">// Create and initialize command to read customers
</SPAN>        Command read = das.getCommand(<SPAN class="code-quote">&quot;getFirstCustomer&quot;</SPAN>);

        <SPAN class="code-comment">// Read
</SPAN>        DataObject root = read.executeQuery();

        <SPAN class="code-comment">//Modify
</SPAN>        root.setString(<SPAN class="code-quote">&quot;CUSTOMER[1]/LASTNAME&quot;</SPAN>, <SPAN class="code-quote">&quot;Some <SPAN class="code-keyword">new</SPAN> name&quot;</SPAN>);

        das.applyChanges(root);
</PRE>
</DIV></DIV>
<P>This example just shows a simple read of a customer DataObject followed setting the last name to some new value. You will have to take my word for it that the value is not written as-is but is instead obfuscated. The actual value written to the database table column is &quot;Fbzr arj anzr&quot; based on the ROT13 algorithm used in the converter.</P>

<P>Converters are specified in the configuration, usually as part of a config.xml file.  Here is the config file for the example:</P>
<DIV class="preformatted panel" style="border-width: 1px;"><DIV class="preformattedContent panelContent">
<PRE>&lt;Config xsi:noNamespaceSchemaLocation=&quot;http:///org.apache.tuscany.das.rdb/config.xsd&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;&gt;

  &lt;Command name=&quot;getFirstCustomer&quot; SQL=&quot;Select * from CUSTOMER where ID = 1&quot; kind=&quot;Select&quot;/&gt;

  &lt;Table tableName=&quot;CUSTOMER&quot;&gt;
        &lt;Column columnName=&quot;ID&quot; primaryKey=&quot;true&quot;/&gt;
    &lt;Column columnName=&quot;LASTNAME&quot; converterClassName=&quot;org.apache.tuscany.das.rdb.test.mappings.StringObfuscationConverter&quot;/&gt;
  &lt;/Table&gt;

&lt;/Config&gt;
</PRE>
</DIV></DIV>
<P>Here is the user-defined converter class:</P>
<DIV class="code panel" style="border-width: 1px;"><DIV class="codeContent panelContent">
<PRE class="code-java">
<SPAN class="code-keyword">public</SPAN> class StringObfuscationConverter <SPAN class="code-keyword">implements</SPAN> Converter {

    <SPAN class="code-keyword">public</SPAN> StringObfuscationConverter() {
        <SPAN class="code-keyword">super</SPAN>();
    }

    <SPAN class="code-keyword">public</SPAN> <SPAN class="code-object">Object</SPAN> getPropertyValue(<SPAN class="code-object">Object</SPAN> columnData) {
        <SPAN class="code-keyword">return</SPAN> toRot13((<SPAN class="code-object">String</SPAN>) columnData);
    }

    <SPAN class="code-keyword">public</SPAN> <SPAN class="code-object">Object</SPAN> getColumnValue(<SPAN class="code-object">Object</SPAN> propertyData) {
        <SPAN class="code-keyword">return</SPAN> toRot13((<SPAN class="code-object">String</SPAN>) propertyData);
    }

    <SPAN class="code-comment">// Utilities
</SPAN>
    <SPAN class="code-comment">// A simple, reversible, obfuscation algorithm using a ROT13 implementation
</SPAN>    <SPAN class="code-keyword">private</SPAN> <SPAN class="code-object">String</SPAN> toRot13(<SPAN class="code-object">String</SPAN> original) {

        <SPAN class="code-object">int</SPAN> abyte = 0;
        <SPAN class="code-object">byte</SPAN>[] buffer = {};
        <SPAN class="code-keyword">try</SPAN> {
            buffer = original.getBytes(<SPAN class="code-quote">&quot;ISO-8859-1&quot;</SPAN>);
        } <SPAN class="code-keyword">catch</SPAN> (UnsupportedEncodingException e) {
            <SPAN class="code-keyword">throw</SPAN> <SPAN class="code-keyword">new</SPAN> Error(e);
        }

        <SPAN class="code-keyword">for</SPAN> (<SPAN class="code-object">int</SPAN> i = 0; i &lt; buffer.length; i++) {
            abyte = buffer[i];
            <SPAN class="code-object">int</SPAN> cap = abyte &amp; 32;
            abyte &amp;= ~cap;
            abyte = ((abyte &gt;= 'A') &amp;&amp; (abyte &lt;= 'Z') ? ((abyte - 'A' + 13) % 26 + 'A') : abyte) | cap;
            buffer[i] = (<SPAN class="code-object">byte</SPAN>) abyte;
        }
        <SPAN class="code-keyword">try</SPAN> {
            <SPAN class="code-keyword">return</SPAN> <SPAN class="code-keyword">new</SPAN> <SPAN class="code-object">String</SPAN>(buffer, <SPAN class="code-quote">&quot;ISO-8859-1&quot;</SPAN>);
        } <SPAN class="code-keyword">catch</SPAN> (UnsupportedEncodingException e) {
            <SPAN class="code-keyword">throw</SPAN> <SPAN class="code-keyword">new</SPAN> Error(e);
        }
    }
}
</PRE>
</DIV></DIV>
							</DIV>
						</DIV>
					</DIV>	
		            <!-- pageContent macro end -->
					
                </TD>
            </TR>
        </TABLE>

		<!-- footer macro -->
				<SCRIPT src="http://www.google-analytics.com/urchin.js" type="text/javascript">
		</SCRIPT>
		<SCRIPT type="text/javascript">
		   _uacct = "UA-1174707-5";
		   urchinTracker();
		</SCRIPT>
		
				<A href="http://www.statcounter.com/" target="_blank"><IMG src="http://c26.statcounter.com/counter.php?sc_project=2619156&java=0&security=94bd7e7d&invisible=0" alt="website stats" border="0"></A>    

		<DIV class="footer">
			Copyright � 2003-2012, The Apache Software Foundation&nbsp;&nbsp;</BR>
                        Apache Tuscany and the Apache Tuscany project logo are trademarks of The Apache Software Foundation.
		</DIV>
		<!-- footer macro end -->

    </BODY>
</HTML>