1 /*
2  * JasperReports - Free Java Reporting Library.
3  * Copyright (C) 2001 - 2019 TIBCO Software Inc. All rights reserved.
4  * http://www.jaspersoft.com
5  *
6  * Unless you have purchased a commercial license agreement from Jaspersoft,
7  * the following license terms apply:
8  *
9  * This program is part of JasperReports.
10  *
11  * JasperReports is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * JasperReports is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * along with JasperReports. If not, see <http://www.gnu.org/licenses/>.
23  */

24 package net.sf.jasperreports.repo;
25
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.io.OutputStream;
29 import java.io.Serializable;
30
31 import net.sf.jasperreports.engine.JRException;
32 import net.sf.jasperreports.engine.JRRuntimeException;
33 import net.sf.jasperreports.engine.util.JRLoader;
34 import net.sf.jasperreports.engine.util.JRSaver;
35
36
37 /**
38  * @author Teodor Danciu (teodord@users.sourceforge.net)
39  */

40 public class SerializedObjectPersistenceService implements PersistenceService
41 {
42
43     @Override
44     public Resource load(String uri, RepositoryService repositoryService)
45     {
46         return load(null, uri, repositoryService);
47     }
48
49     @Override
50     public Resource load(RepositoryContext context, String uri, RepositoryService repositoryService)
51     {
52         SerializableResource<Serializable> resource = null
53
54         InputStreamResource isResource = repositoryService.getResource(context, uri, InputStreamResource.class);
55         
56         InputStream is = isResource == null ? null : isResource.getInputStream();
57         if (is != null)
58         {
59             resource = new SerializableResource<Serializable>();
60             try
61             {
62                 resource.setValue((Serializable)JRLoader.loadObject(is));
63             }
64             catch (JRException e)
65             {
66                 throw new JRRuntimeException(e);
67             }
68             finally
69             {
70                 try
71                 {
72                     is.close();
73                 }
74                 catch (IOException e)
75                 {
76                 }
77             }
78         }
79
80         return resource;
81     }
82     
83     @Override
84     public void save(Resource resource, String uri, RepositoryService repositoryService)
85     {
86         @SuppressWarnings("unchecked")
87         ObjectResource<Object> objectResource = (ObjectResource<Object>)resource;
88         
89         OutputStreamResource osResource = repositoryService.getResource(uri, OutputStreamResource.class);
90         
91         OutputStream os = osResource == null ? null : osResource.getOutputStream();
92         if (os != null)
93         {
94             try
95             {
96                 JRSaver.saveObject(objectResource.getValue(), os);
97             }
98             catch (JRException e)
99             {
100                 throw new JRRuntimeException(e);
101             }
102             finally
103             {
104                 try
105                 {
106                     os.close();
107                 }
108                 catch (IOException e)
109                 {
110                 }
111             }
112         }
113         
114     }
115     
116 }
117