1 16 17 package org.apache.jetspeed.services.psmlmanager.db; 18 19 import java.io.Reader ; 21 import java.io.StringReader ; 22 import java.io.StringWriter ; 23 import java.io.IOException ; 24 25 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 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 45 public class DBUtils 46 { 47 50 private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(DBUtils.class.getName()); 51 52 58 public static Portlets bytesToPortlets(byte[] portletBytes, Mapping mapping) 59 { 60 Reader reader = new StringReader (new String (portletBytes)); 61 try 62 { 63 Unmarshaller unmarshaller = new Unmarshaller((Mapping)mapping); 64 return (Portlets)unmarshaller.unmarshal(reader); 65 66 } 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 e) 87 { 88 logger.error("", e); 89 } 90 } 91 return null; } 93 94 100 public static byte[] portletsToBytes(Portlets portlets, Mapping mapping) 101 { 102 if (portlets == null) 103 { 104 String message = "PSMLManager: Must specify portlets"; 105 logger.error( message ); 106 throw new IllegalArgumentException ( message ); 107 } 108 109 StringWriter writer = new StringWriter (); 110 try 111 { 112 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 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 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 e) 147 { 148 logger.error("", e); 149 } 150 } 151 return null; } 153 154 } 155 | Popular Tags |