KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jetspeed > services > psmlmanager > db > DBUtils


1 /*
2  * Copyright 2000-2001,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16  
17 package org.apache.jetspeed.services.psmlmanager.db;
18
19 //standard java stuff
20
import java.io.Reader JavaDoc;
21 import java.io.StringReader JavaDoc;
22 import java.io.StringWriter JavaDoc;
23 import java.io.IOException JavaDoc;
24
25 // Jetspeed classes
26
import org.apache.jetspeed.om.profile.Portlets;
27 import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
28 import org.apache.jetspeed.services.logging.JetspeedLogger;
29
30 //castor support
31
import org.exolab.castor.xml.Unmarshaller;
32 import org.exolab.castor.xml.Marshaller;
33
34 import org.exolab.castor.xml.MarshalException;
35 import org.exolab.castor.mapping.Mapping;
36 import org.exolab.castor.mapping.MappingException;
37 import org.exolab.castor.xml.ValidationException;
38
39 /**
40  * This is a utility class used for database PSML implementation.
41  *
42  * @author <a HREF="mailto:adambalk@cisco.com">Atul Dambalkar</a>
43  * @version $Id: DBUtils.java,v 1.7 2004/02/23 03:32:19 jford Exp $
44  */

45 public class DBUtils
46 {
47     /**
48      * Static initialization of the logger for this class
49      */

50     private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(DBUtils.class.getName());
51     
52     /** Deserialize a PSML structure read from bytes array using Castor
53      * XML unmarshaller
54      *
55      * @param portletBytes Bytes array to load the PSML from
56      * @return PSML structure Portlets object
57      */

58     public static Portlets bytesToPortlets(byte[] portletBytes, Mapping mapping)
59     {
60         Reader JavaDoc reader = new StringReader JavaDoc(new String JavaDoc(portletBytes));
61         try
62         {
63             Unmarshaller unmarshaller = new Unmarshaller((Mapping)mapping);
64             return (Portlets)unmarshaller.unmarshal(reader);
65
66 // return Portlets.unmarshal(reader);
67
}
68         catch (MarshalException e)
69         {
70             logger.error("PSMLManager: Could not unmarshal the inputstream ", e);
71         }
72         catch (MappingException e)
73         {
74             logger.error("PSMLManager: Could not unmarshal the inputstream ", e);
75         }
76
77         catch (ValidationException e)
78         {
79             logger.error("PSMLManager: document is not valid", e);
80         }
81         finally
82         {
83             try {
84                 reader.close();
85             }
86             catch (IOException JavaDoc e)
87             {
88                 logger.error("", e);
89             }
90         }
91         return null; // control shouldn't arrive here
92
}
93
94     /** Serialize a PSML structure using string writer with Castor XML
95      * marshaller, put it in bytes array and return it.
96      *
97      * @param portlets the structure to convert to bytes array
98      * @return Bytes array object for portles
99      */

100     public static byte[] portletsToBytes(Portlets portlets, Mapping mapping)
101     {
102         if (portlets == null)
103         {
104             String JavaDoc message = "PSMLManager: Must specify portlets";
105             logger.error( message );
106             throw new IllegalArgumentException JavaDoc( message );
107         }
108
109         StringWriter JavaDoc writer = new StringWriter JavaDoc();
110         try
111         {
112 // portlets.marshal(writer);
113

114             Marshaller marshaller = new Marshaller(writer);
115             marshaller.setMapping(mapping);
116             marshaller.marshal(portlets);
117             
118             if (logger.isDebugEnabled())
119                 logger.debug("Portlets: " + writer.toString());
120
121             /**** Platform's default character encoding will be used ****/
122             return writer.toString().getBytes();
123         }
124         catch (MarshalException e)
125         {
126             logger.error("PSMLManager: Could not marshal the stringwriter ", e);
127         }
128         catch (IOException JavaDoc e)
129         {
130             logger.error("PSMLManager: Could not marshal the stringwriter ", e);
131         }
132         catch (MappingException e)
133         {
134             logger.error("PSMLManager: Could not marshal the stringwriter ", e);
135         }
136         catch (ValidationException e)
137         {
138             logger.error("PSMLManager: document is not valid", e);
139         }
140         finally
141         {
142             try
143             {
144                 writer.close();
145             }
146             catch (IOException JavaDoc e)
147             {
148                 logger.error("", e);
149             }
150         }
151         return null; // control shouldn't arrive here
152
}
153
154 }
155
Popular Tags