001/* 002 * (C) Copyright 2006-2007 Nuxeo SAS (http://nuxeo.com/) and contributors. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl.html 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * Contributors: 015 * <a href="mailto:at@nuxeo.com">Anahide Tchertchian</a> 016 * 017 * $Id: AbstractIOResourceAdapter.java 25080 2007-09-18 14:52:20Z atchertchian $ 018 */ 019 020package org.nuxeo.ecm.platform.io.api; 021 022import java.io.InputStream; 023import java.io.OutputStream; 024import java.io.Serializable; 025import java.util.Collection; 026import java.util.HashMap; 027import java.util.Map; 028 029import org.apache.commons.logging.Log; 030import org.apache.commons.logging.LogFactory; 031import org.nuxeo.ecm.core.api.DocumentRef; 032import org.nuxeo.ecm.core.io.DocumentTranslationMap; 033 034/** 035 * Abstract implementation for {@link IOResourceAdapter}. 036 * <p> 037 * Offers helper methods for properties. 038 * 039 * @author <a href="mailto:at@nuxeo.com">Anahide Tchertchian</a> 040 */ 041public abstract class AbstractIOResourceAdapter implements IOResourceAdapter { 042 043 private static final long serialVersionUID = 4399167777434048174L; 044 045 private static final Log log = LogFactory.getLog(AbstractIOResourceAdapter.class); 046 047 protected Map<String, Serializable> properties; 048 049 protected AbstractIOResourceAdapter() { 050 properties = new HashMap<String, Serializable>(); 051 } 052 053 public Map<String, Serializable> getProperties() { 054 return properties; 055 } 056 057 public void setProperties(Map<String, Serializable> properties) { 058 this.properties = properties; 059 } 060 061 public abstract IOResources extractResources(String repo, Collection<DocumentRef> sources); 062 063 public abstract IOResources translateResources(String repo, IOResources resources, DocumentTranslationMap map); 064 065 public abstract void storeResources(IOResources newResources); 066 067 public abstract void getResourcesAsXML(OutputStream out, IOResources newResources); 068 069 public abstract IOResources loadResourcesFromXML(InputStream stream); 070 071 // helper methods 072 073 protected boolean getBooleanProperty(String propName) { 074 Boolean value = (Boolean) properties.get(propName); 075 if (value == null) { 076 return false; 077 } 078 return value; 079 } 080 081 protected void setBooleanProperty(String propName, Serializable propValue) { 082 if (propValue == null) { 083 return; 084 } 085 if (propValue instanceof String) { 086 properties.put(propName, Boolean.valueOf((String) propValue)); 087 } else { 088 log.error(String.format("Property %s needs a string representing a boolean:" + " invalid value %s", 089 propName, propValue)); 090 } 091 } 092 093 protected String getStringProperty(String propName) { 094 return (String) properties.get(propName); 095 } 096 097 protected void setStringProperty(String propName, Serializable propValue) { 098 if (propValue == null) { 099 return; 100 } 101 if (!(propValue instanceof String)) { 102 log.error(String.format("Property %s needs a string value:" + " invalid value %s", propName, propValue)); 103 } 104 properties.put(propName, propValue); 105 } 106 107 protected String[] getStringArrayProperty(String propName) { 108 return (String[]) properties.get(propName); 109 } 110 111 protected void setStringArrayProperty(String propName, Serializable propValue) { 112 if (propValue == null) { 113 return; 114 } 115 if (!(propValue instanceof String[])) { 116 log.error(String.format("Property %s needs a string array, invalid value %s", propName, propValue)); 117 } 118 properties.put(propName, propValue); 119 } 120 121}