From 05c5f8a0aecfb69205a4b2cb3179a757595d6ed3 Mon Sep 17 00:00:00 2001 From: felmer <felmer> Date: Thu, 11 Nov 2010 08:18:29 +0000 Subject: [PATCH] LMS-1870 add mapper for serializable objects SVN: 18622 --- .../db/SerializableObjectMapper.java | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/SerializableObjectMapper.java diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/SerializableObjectMapper.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/SerializableObjectMapper.java new file mode 100644 index 00000000000..d12ea3bdbb7 --- /dev/null +++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/SerializableObjectMapper.java @@ -0,0 +1,58 @@ +/* + * Copyright 2010 ETH Zuerich, CISD + * + * Licensed 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. + */ + +package ch.systemsx.cisd.openbis.generic.server.dataaccess.db; + +import java.io.Serializable; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Types; + +import net.lemnik.eodsql.TypeMapper; + +import org.apache.commons.lang.SerializationUtils; + +/** + * + * + * @author Franz-Josef Elmer + */ +public class SerializableObjectMapper implements TypeMapper<Serializable> +{ + + public Serializable get(ResultSet results, int column) throws SQLException + { + return (Serializable) SerializationUtils.deserialize(results.getBytes(column)); + } + + public void set(ResultSet results, int column, Serializable obj) throws SQLException + { + results.updateBytes(column, SerializationUtils.serialize(obj)); + } + + public void set(PreparedStatement statement, int column, Serializable obj) throws SQLException + { + if (obj != null) + { + statement.setBytes(column, SerializationUtils.serialize(obj)); + } else + { + statement.setNull(column, Types.BINARY); + } + } + +} -- GitLab