Project

General

Profile

1
/**
2
 *  '$RCSfile$'
3
 *  Copyright: 2000 Regents of the University of California and the
4
 *              National Center for Ecological Analysis and Synthesis
5
 *
6
 *   '$Author: $'
7
 *     '$Date: 2009-06-13 15:28:13 +0300  $'
8
 *
9
 * This program is free software; you can redistribute it and/or modify
10
 * it under the terms of the GNU General Public License as published by
11
 * the Free Software Foundation; either version 2 of the License, or
12
 * (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with this program; if not, write to the Free Software
21
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22
 */
23
package edu.ucsb.nceas.metacat.dataone;
24

    
25
import java.io.ByteArrayOutputStream;
26
import java.io.File;
27
import java.io.FileNotFoundException;
28
import java.io.FileOutputStream;
29
import java.io.IOException;
30
import java.io.InputStream;
31
import java.io.OutputStream;
32
import java.io.PrintWriter;
33
import java.sql.SQLException;
34
import java.util.Date;
35
import java.util.Enumeration;
36
import java.util.Hashtable;
37
import java.util.Timer;
38

    
39
import javax.servlet.ServletContext;
40
import javax.servlet.http.HttpServletRequest;
41
import javax.servlet.http.HttpServletResponse;
42

    
43
import org.apache.commons.io.IOUtils;
44
import org.apache.log4j.Logger;
45
import org.dataone.service.exceptions.IdentifierNotUnique;
46
import org.dataone.service.exceptions.InsufficientResources;
47
import org.dataone.service.exceptions.InvalidRequest;
48
import org.dataone.service.exceptions.InvalidSystemMetadata;
49
import org.dataone.service.exceptions.InvalidToken;
50
import org.dataone.service.exceptions.NotAuthorized;
51
import org.dataone.service.exceptions.NotFound;
52
import org.dataone.service.exceptions.NotImplemented;
53
import org.dataone.service.exceptions.ServiceFailure;
54
import org.dataone.service.exceptions.UnsupportedType;
55
import org.dataone.service.mn.MemberNodeCrud;
56
import org.dataone.service.types.AuthToken;
57
import org.dataone.service.types.Checksum;
58
import org.dataone.service.types.DescribeResponse;
59
import org.dataone.service.types.Identifier;
60
import org.dataone.service.types.LogRecordSet;
61
import org.dataone.service.types.SystemMetadata;
62
import org.jibx.runtime.BindingDirectory;
63
import org.jibx.runtime.IBindingFactory;
64
import org.jibx.runtime.IMarshallingContext;
65
import org.jibx.runtime.IUnmarshallingContext;
66
import org.jibx.runtime.JiBXException;
67

    
68
import com.gc.iotools.stream.is.InputStreamFromOutputStream;
69

    
70
import edu.ucsb.nceas.metacat.AccessionNumberException;
71
import edu.ucsb.nceas.metacat.DocumentImpl;
72
import edu.ucsb.nceas.metacat.EventLog;
73
import edu.ucsb.nceas.metacat.IdentifierManager;
74
import edu.ucsb.nceas.metacat.McdbDocNotFoundException;
75
import edu.ucsb.nceas.metacat.McdbException;
76
import edu.ucsb.nceas.metacat.MetacatHandler;
77
import edu.ucsb.nceas.metacat.client.InsufficientKarmaException;
78
import edu.ucsb.nceas.metacat.properties.PropertyService;
79
import edu.ucsb.nceas.metacat.replication.ForceReplicationHandler;
80
import edu.ucsb.nceas.metacat.service.SessionService;
81
import edu.ucsb.nceas.metacat.util.DocumentUtil;
82
import edu.ucsb.nceas.metacat.util.SessionData;
83
import edu.ucsb.nceas.utilities.ParseLSIDException;
84
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
85

    
86
/**
87
 * 
88
 * Implements DataONE MemberNode CRUD API for Metacat. 
89
 * 
90
 * @author Matthew Jones
91
 */
92
public class CrudService implements MemberNodeCrud {
93

    
94
    /*private ServletContext servletContext;
95
    private HttpServletRequest request;
96
    private HttpServletResponse response;*/
97
    
98
    private static CrudService crudService = null;
99

    
100
    private MetacatHandler handler;
101
    private Hashtable<String, String[]> params;
102
    Logger logMetacat = null;
103
    
104
    private String metacatUrl;
105

    
106
    /**
107
     * singleton accessor
108
     */
109
    public static CrudService getInstance()
110
    {
111
      if(crudService == null)
112
      {
113
        crudService = new CrudService();
114
      }
115
      
116
      return crudService;
117
    }
118
    
119
    /**
120
     * Initializes new instance by setting servlet context,request and response.
121
     * TODO: remove dependency on Servlet infrastructure
122
     * TODO: Make this a real service, and make it a Singleton
123
     */
124
    public CrudService() {
125
    //change crud service into a singleton.  dont pass servlet data structures here
126
        logMetacat = Logger.getLogger(CrudService.class);
127
        try
128
        {
129
            String server = PropertyService.getProperty("server.name");
130
            String port = PropertyService.getProperty("server.httpPort");
131
            String context = PropertyService.getProperty("application.context");
132
            metacatUrl = "http://" + server + ":" + port + "/" + context;
133
            logMetacat.debug("Initializing CrudService with url " + metacatUrl);
134
        }
135
        catch(Exception e)
136
        {
137
            logMetacat.error("Could not find servlet url in CrudService: " + e.getMessage());
138
            e.printStackTrace();
139
            throw new RuntimeException("Error getting servlet url in CrudService: " + e.getMessage());
140
        }
141
        
142
        /*this.servletContext = servletContext;
143
        this.request = request;
144
        this.response = response;*/
145
        
146
        params = new Hashtable<String, String[]>();
147

    
148
        handler = new MetacatHandler(new Timer());
149

    
150
    }
151
    
152
    /**
153
     * return the context url CrudService is using.
154
     */
155
    public String getContextUrl()
156
    {
157
        return metacatUrl;
158
    }
159
    
160
    /**
161
     * set the params for this service from an HttpServletRequest param list
162
     */
163
    public void setParamsFromRequest(HttpServletRequest request)
164
    {
165
        Enumeration paramlist = request.getParameterNames();
166
        while (paramlist.hasMoreElements()) {
167
            String name = (String) paramlist.nextElement();
168
            String[] value = (String[])request.getParameterValues(name);
169
            params.put(name, value);
170
        }
171
    }
172
    
173
    /**
174
     * set the parameter values needed for this request
175
     */
176
    public void setParameter(String name, String[] value)
177
    {
178
        params.put(name, value);
179
    }
180
    
181
    public Identifier create(AuthToken token, Identifier guid, 
182
            InputStream object, SystemMetadata sysmeta) throws InvalidToken, 
183
            ServiceFailure, NotAuthorized, IdentifierNotUnique, UnsupportedType, 
184
            InsufficientResources, InvalidSystemMetadata, NotImplemented {
185

    
186
        logMetacat.debug("Starting CrudService.create()...");
187
        
188
        // authenticate & get user info
189
        SessionData sessionData = getSessionData(token);
190
        String username = sessionData.getUserName();
191
        String[] groups = sessionData.getGroupNames();
192

    
193
        // verify that guid == SystemMetadata.getIdentifier()
194
        logMetacat.debug("Comparing guid|sysmeta_guid: " + guid.getValue() + "|" + sysmeta.getIdentifier().getValue());
195
        if (!guid.getValue().equals(sysmeta.getIdentifier().getValue())) {
196
            throw new InvalidSystemMetadata(1180, 
197
                "GUID in method call does not match GUID in system metadata.");
198
        }
199

    
200
        logMetacat.debug("Checking if identifier exists...");
201
        // Check that the identifier does not already exist
202
        IdentifierManager im = IdentifierManager.getInstance();
203
        if (im.identifierExists(guid.getValue())) {
204
            throw new IdentifierNotUnique(1120, 
205
                "GUID is already in use by an existing object.");
206
        }
207

    
208
        // Check if we are handling metadata or data
209
        boolean isScienceMetadata = isScienceMetadata(sysmeta);
210
        
211
        if (isScienceMetadata) {
212
            // CASE METADATA:
213
            try {
214
                String xml = IOUtils.toString(object);
215
                this.insertDocument(xml, guid, sessionData);
216
            } catch (IOException e) {
217
                String msg = "Could not create string from XML stream: " +
218
                    " " + e.getMessage();
219
                logMetacat.debug(msg);
220
                throw new ServiceFailure(1190, msg);
221
            }
222

    
223
        } else {
224
            // DEFAULT CASE: DATA (needs to be checked and completed)
225
            insertDataObject(object, guid, sessionData);
226
            
227
        }
228

    
229
        // For Metadata and Data, insert the system metadata into the object store too
230
        insertSystemMetadata(sysmeta, sessionData);
231

    
232
        logMetacat.debug("Returning from CrudService.create()");
233
        return guid;
234
    }
235

    
236
    public Identifier delete(AuthToken token, Identifier guid)
237
            throws InvalidToken, ServiceFailure, NotAuthorized, NotFound, 
238
            NotImplemented {
239
        throw new NotImplemented(1000, "This method not yet implemented.");
240
    }
241

    
242
    public DescribeResponse describe(AuthToken token, Identifier guid)
243
            throws InvalidToken, ServiceFailure, NotAuthorized, NotFound, 
244
            NotImplemented {
245
        throw new NotImplemented(1000, "This method not yet implemented.");
246
    }
247
    
248
    public InputStream get(AuthToken token, Identifier guid)
249
            throws InvalidToken, ServiceFailure, NotAuthorized, NotFound, 
250
            NotImplemented {
251
        
252
        // Retrieve the session information from the AuthToken
253
        // If the session is expired, then the user is 'public'
254
        final SessionData sessionData = getSessionData(token);
255
        
256
        // Look up the localId for this global identifier
257
        IdentifierManager im = IdentifierManager.getInstance();
258
        try {
259
            final String localId = im.getLocalId(guid.getValue());
260

    
261
            final InputStreamFromOutputStream<String> objectStream = 
262
                new InputStreamFromOutputStream<String>() {
263
                
264
                @Override
265
                public String produce(final OutputStream dataSink) throws Exception {
266

    
267
                    try {
268
                        handler.readFromMetacat(/*request.getRemoteAddr()*/metacatUrl, null, 
269
                                dataSink, localId, "xml",
270
                                sessionData.getUserName(), 
271
                                sessionData.getGroupNames(), true, params);
272
                    } catch (PropertyNotFoundException e) {
273
                        throw new ServiceFailure(1030, e.getMessage());
274
                    } catch (ClassNotFoundException e) {
275
                        throw new ServiceFailure(1030, e.getMessage());
276
                    } catch (IOException e) {
277
                        throw new ServiceFailure(1030, e.getMessage());
278
                    } catch (SQLException e) {
279
                        throw new ServiceFailure(1030, e.getMessage());
280
                    } catch (McdbException e) {
281
                        throw new ServiceFailure(1030, e.getMessage());
282
                    } catch (ParseLSIDException e) {
283
                        throw new NotFound(1020, e.getMessage());
284
                    } catch (InsufficientKarmaException e) {
285
                        throw new NotAuthorized(1000, "Not authorized for get().");
286
                    }
287

    
288
                    return "Completed";
289
                }
290
            };
291
            return objectStream;
292

    
293
        } catch (McdbDocNotFoundException e) {
294
            throw new NotFound(1020, e.getMessage());
295
        }
296
    }
297

    
298
    public Checksum getChecksum(AuthToken token, Identifier guid)
299
            throws InvalidToken, ServiceFailure, NotAuthorized, NotFound, 
300
            InvalidRequest, NotImplemented {
301
        throw new NotImplemented(1000, "This method not yet implemented.");
302
    }
303

    
304
    public Checksum getChecksum(AuthToken token, Identifier guid, 
305
            String checksumAlgorithm) throws InvalidToken, ServiceFailure, 
306
            NotAuthorized, NotFound, InvalidRequest, NotImplemented {
307
        throw new NotImplemented(1000, "This method not yet implemented.");
308
    }
309

    
310
    public LogRecordSet getLogRecords(AuthToken token, Date fromDate, Date toDate)
311
            throws InvalidToken, ServiceFailure, NotAuthorized, InvalidRequest, 
312
            NotImplemented {
313
        throw new NotImplemented(1000, "This method not yet implemented.");
314
    }
315

    
316
    public SystemMetadata getSystemMetadata(AuthToken token, Identifier guid)
317
            throws InvalidToken, ServiceFailure, NotAuthorized, NotFound, 
318
            InvalidRequest, NotImplemented {
319
        
320
        logMetacat.debug("CrudService.getSystemMetadata - for guid: " + guid.getValue());
321
        
322
        // Retrieve the session information from the AuthToken
323
        // If the session is expired, then the user is 'public'
324
        final SessionData sessionData = getSessionData(token);
325

    
326
        // TODO: Check access control rules
327
                
328
        try {
329
            IdentifierManager im = IdentifierManager.getInstance();
330
            // This is a test id of an existing sysmeta document -- for temporary testing
331
            String testGuid = "autogen.20101192441566.1";
332
            // TODO: Look up ID of system metadata based on guid
333
            // TODO: Initially from document, later from entry in table?
334
//            String localId = im.getLocalId(guid.getValue());
335
            final String localId = im.getLocalId(testGuid);
336
            
337
            // Read system metadata from metacat's db
338
            final InputStreamFromOutputStream<String> objectStream = 
339
                new InputStreamFromOutputStream<String>() {
340
                
341
                @Override
342
                public String produce(final OutputStream dataSink) throws Exception {
343
                    try {
344
                        handler.readFromMetacat(/*request.getRemoteAddr()*/metacatUrl, null, 
345
                                dataSink, localId, "xml",
346
                                sessionData.getUserName(), 
347
                                sessionData.getGroupNames(), true, params);
348
                    } catch (PropertyNotFoundException e) {
349
                        throw new ServiceFailure(1030, e.getMessage());
350
                    } catch (ClassNotFoundException e) {
351
                        throw new ServiceFailure(1030, e.getMessage());
352
                    } catch (IOException e) {
353
                        throw new ServiceFailure(1030, e.getMessage());
354
                    } catch (SQLException e) {
355
                        throw new ServiceFailure(1030, e.getMessage());
356
                    } catch (McdbException e) {
357
                        throw new ServiceFailure(1030, e.getMessage());
358
                    } catch (ParseLSIDException e) {
359
                        throw new NotFound(1020, e.getMessage());
360
                    } catch (InsufficientKarmaException e) {
361
                        throw new NotAuthorized(1000, "Not authorized for get().");
362
                    }
363

    
364
                    return "Completed";
365
                }
366
            };
367
            
368
            // Deserialize the xml to create a SystemMetadata object
369
            SystemMetadata sysmeta = deserializeSystemMetadata(objectStream);
370
            return sysmeta;
371
            
372
        } catch (McdbDocNotFoundException e) {
373
            throw new NotFound(1000, e.getMessage());
374
        }                
375
    }
376

    
377
    public Identifier update(AuthToken token, Identifier guid, 
378
            InputStream object, Identifier obsoletedGuid, SystemMetadata sysmeta) 
379
            throws InvalidToken, ServiceFailure, NotAuthorized, IdentifierNotUnique, 
380
            UnsupportedType, InsufficientResources, NotFound, InvalidSystemMetadata, 
381
            NotImplemented {
382
        throw new NotImplemented(1000, "This method not yet implemented.");
383
    }
384

    
385
    /*
386
     * Look up the information on the session using the token provided in
387
     * the AuthToken.  The Session should have all relevant user information.
388
     * If the session has expired or is invalid, the 'public' session will
389
     * be returned, giving the user anonymous access.
390
     */
391
    private static SessionData getSessionData(AuthToken token) {
392
        SessionData sessionData = null;
393
        String sessionId = "PUBLIC";
394
        if (token != null) {
395
            sessionId = token.getToken();
396
        }
397
        
398
        // if the session id is registered in SessionService, get the
399
        // SessionData for it. Otherwise, use the public session.
400
        if (SessionService.isSessionRegistered(sessionId)) {
401
            sessionData = SessionService.getRegisteredSession(sessionId);
402
        } else {
403
            sessionData = SessionService.getPublicSession();
404
        }
405
        
406
        return sessionData;
407
    }
408

    
409
    /** 
410
     * Determine if a given object should be treated as an XML science metadata
411
     * object. 
412
     * 
413
     * TODO: This test should be externalized in a configuration dictionary rather than being hardcoded.
414
     * 
415
     * @param sysmeta the SystemMetadata describig the object
416
     * @return true if the object should be treated as science metadata
417
     */
418
    private boolean isScienceMetadata(SystemMetadata sysmeta) {
419
        boolean scimeta = false;
420
        switch (sysmeta.getObjectFormat()) {
421
            case EML_2_1_0: scimeta = true; break;
422
            case EML_2_0_1: scimeta = true; break;
423
            case EML_2_0_0: scimeta = true; break;
424
            case FGDC_STD_001_1_1999: scimeta = true; break;
425
            case FGDC_STD_001_1998: scimeta = true; break;
426
            case NCML_2_2: scimeta = true; break;
427
        }
428
        
429
        return scimeta;
430
    }
431

    
432
    private void insertDataObject(InputStream object, Identifier guid, 
433
            SessionData sessionData) throws ServiceFailure {
434
        
435
        String username = sessionData.getUserName();
436
        String[] groups = sessionData.getGroupNames();
437

    
438
        // generate guid/localId pair for object
439
        logMetacat.debug("Generating a guid/localId mapping");
440
        IdentifierManager im = IdentifierManager.getInstance();
441
        String localId = im.generateLocalId(guid.getValue(), 1);
442

    
443
        try {
444
            logMetacat.debug("Case DATA: starting to write to disk.");
445
            if (DocumentImpl.getDataFileLockGrant(localId)) {
446
    
447
                // Save the data file to disk using "localId" as the name
448
                try {
449
                    String datafilepath = PropertyService.getProperty("application.datafilepath");
450
    
451
                    File dataDirectory = new File(datafilepath);
452
                    dataDirectory.mkdirs();
453
    
454
                    File newFile = writeStreamToFile(dataDirectory, localId, object);
455
    
456
                    // TODO: Check that the file size matches SystemMetadata
457
                    //                        long size = newFile.length();
458
                    //                        if (size == 0) {
459
                    //                            throw new IOException("Uploaded file is 0 bytes!");
460
                    //                        }
461
    
462
                    // Register the file in the database (which generates an exception
463
                    // if the localId is not acceptable or other untoward things happen
464
                    try {
465
                        logMetacat.debug("Registering document...");
466
                        DocumentImpl.registerDocument(localId, "BIN", localId,
467
                                username, groups);
468
                        logMetacat.debug("Registration step completed.");
469
                    } catch (SQLException e) {
470
                        //newFile.delete();
471
                        logMetacat.debug("SQLE: " + e.getMessage());
472
                        e.printStackTrace(System.out);
473
                        throw new ServiceFailure(1190, "Registration failed: " + e.getMessage());
474
                    } catch (AccessionNumberException e) {
475
                        //newFile.delete();
476
                        logMetacat.debug("ANE: " + e.getMessage());
477
                        e.printStackTrace(System.out);
478
                        throw new ServiceFailure(1190, "Registration failed: " + e.getMessage());
479
                    } catch (Exception e) {
480
                        //newFile.delete();
481
                        logMetacat.debug("Exception: " + e.getMessage());
482
                        e.printStackTrace(System.out);
483
                        throw new ServiceFailure(1190, "Registration failed: " + e.getMessage());
484
                    }
485
    
486
                    logMetacat.debug("Logging the creation event.");
487
                    EventLog.getInstance().log(/*request.getRemoteAddr()*/metacatUrl,
488
                            username, localId, "create");
489
    
490
                    // Schedule replication for this data file
491
                    logMetacat.debug("Scheduling replication.");
492
                    ForceReplicationHandler frh = new ForceReplicationHandler(
493
                            localId, "insert", false, null);
494
    
495
                } catch (PropertyNotFoundException e) {
496
                    throw new ServiceFailure(1190, "Could not lock file for writing:" + e.getMessage());
497
                }
498
    
499
            }
500
        } catch (Exception e) {
501
            // Could not get a lock on the document, so we can not update the file now
502
            throw new ServiceFailure(1190, "Failed to lock file: " + e.getMessage());
503
        }
504
    }
505

    
506
    private File writeStreamToFile(File dir, String fileName, InputStream data) 
507
        throws ServiceFailure {
508
        
509
        File newFile = new File(dir, fileName);
510
        logMetacat.debug("Filename for write is: " + newFile.getAbsolutePath());
511

    
512
        try {
513
            if (newFile.createNewFile()) {
514
                // write data stream to desired file
515
                OutputStream os = new FileOutputStream(newFile);
516
                long length = IOUtils.copyLarge(data, os);
517
                os.flush();
518
                os.close();
519
            } else {
520
                logMetacat.debug("File creation failed, or file already exists.");
521
                throw new ServiceFailure(1190, "File already exists: " + fileName);
522
            }
523
        } catch (FileNotFoundException e) {
524
            logMetacat.debug("FNF: " + e.getMessage());
525
            throw new ServiceFailure(1190, "File not found: " + fileName + " " 
526
                    + e.getMessage());
527
        } catch (IOException e) {
528
            logMetacat.debug("IOE: " + e.getMessage());
529
            throw new ServiceFailure(1190, "File was not written: " + fileName 
530
                    + " " + e.getMessage());
531
        }
532

    
533
        return newFile;
534
    }
535

    
536
    private void insertSystemMetadata(SystemMetadata sysmeta, SessionData sessionData) 
537
        throws ServiceFailure {
538
        logMetacat.debug("Starting to insert SystemMetadata...");
539
    
540
        // generate guid/localId pair for sysmeta
541
        Identifier sysMetaGuid = new Identifier();
542
        sysMetaGuid.setValue(DocumentUtil.generateDocumentId(1));
543
        sysmeta.setDateSysMetadataModified(new Date());
544

    
545
        String xml = new String(serializeSystemMetadata(sysmeta).toByteArray());
546
        String localId = insertDocument(xml, sysMetaGuid, sessionData);
547
        //insert the system metadata doc id into the identifiers table to 
548
        //link it to the data or metadata document
549
        IdentifierManager.getInstance().createSystemMetadataMapping(sysMetaGuid.getValue(), localId);
550
    }
551
    
552
    /**
553
     * insert a document, return the id of the document that was inserted
554
     */
555
    private String insertDocument(String xml, Identifier guid, SessionData sessionData) 
556
        throws ServiceFailure {
557
        logMetacat.debug("Starting to insert xml document...");
558
        IdentifierManager im = IdentifierManager.getInstance();
559

    
560
        // generate guid/localId pair for sysmeta
561
        String localId = im.generateLocalId(guid.getValue(), 1);
562
        logMetacat.debug("Metadata guid|localId: " + guid.getValue() + "|" +
563
                localId);
564

    
565
        String[] action = new String[1];
566
        action[0] = "insert";
567
        params.put("action", action);
568
        String[] docid = new String[1];
569
        docid[0] = localId;
570
        params.put("docid", docid);
571
        String[] doctext = new String[1];
572
        doctext[0] = xml;
573
        logMetacat.debug(doctext[0]);
574
        params.put("doctext", doctext);
575
        
576
        // TODO: refactor handleInsertOrUpdateAction() to not output XML directly
577
        // onto output stream, or alternatively, capture that and parse it to 
578
        // generate the right exceptions
579
        //ByteArrayOutputStream output = new ByteArrayOutputStream();
580
        //PrintWriter pw = new PrintWriter(output);
581
        String result = handler.handleInsertOrUpdateAction(/*request.getRemoteAddr()*/metacatUrl, null, 
582
                            null, params, sessionData.getUserName(), sessionData.getGroupNames());
583
        //String outputS = new String(output.toByteArray());
584
        logMetacat.debug("CrudService.insertDocument - Metacat returned: " + result);
585
//        if (!(outputS.indexOf("<success>") > 0 && outputS.indexOf(localId) > 0)) {
586
//            throw new ServiceFailure(1190, outputS);
587
//        }
588
        logMetacat.debug("Finsished inserting xml document with id " + localId);
589
        return localId;
590
    }
591
    
592
    public static ByteArrayOutputStream serializeSystemMetadata(SystemMetadata sysmeta) 
593
        throws ServiceFailure {
594
        IBindingFactory bfact;
595
        ByteArrayOutputStream sysmetaOut = null;
596
        try {
597
            bfact = BindingDirectory.getFactory(SystemMetadata.class);
598
            IMarshallingContext mctx = bfact.createMarshallingContext();
599
            sysmetaOut = new ByteArrayOutputStream();
600
            mctx.marshalDocument(sysmeta, "UTF-8", null, sysmetaOut);
601
        } catch (JiBXException e) {
602
            throw new ServiceFailure(1190, "Failed to serialize and insert SystemMetadata: " + e.getMessage());
603
        }
604
        
605
        return sysmetaOut;
606
    }
607
    
608
    public static SystemMetadata deserializeSystemMetadata(InputStream xml) 
609
        throws ServiceFailure {
610
        try {
611
            IBindingFactory bfact = BindingDirectory.getFactory(SystemMetadata.class);
612
            IUnmarshallingContext uctx = bfact.createUnmarshallingContext();
613
            SystemMetadata sysmeta = (SystemMetadata) uctx.unmarshalDocument(xml, null);
614
            return sysmeta;
615
        } catch (JiBXException e) {
616
            throw new ServiceFailure(1190, "Failed to serialize and insert SystemMetadata: " + e.getMessage());
617
        }    
618
    }
619
}
    (1-1/1)