001/* 002 * (C) Copyright 2006-2017 Nuxeo (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * bstefanescu 018 */ 019package org.nuxeo.runtime.model.persistence; 020 021import java.io.IOException; 022import java.util.List; 023 024import org.nuxeo.runtime.RuntimeServiceException; 025import org.nuxeo.runtime.model.ComponentContext; 026import org.nuxeo.runtime.model.ComponentInstance; 027import org.nuxeo.runtime.model.DefaultComponent; 028import org.nuxeo.runtime.model.RegistrationInfo; 029import org.nuxeo.runtime.model.RuntimeContext; 030import org.nuxeo.runtime.model.persistence.fs.FileSystemStorage; 031 032/** 033 * @author <a href="mailto:[email protected]">Bogdan Stefanescu</a> 034 */ 035public class ContributionPersistenceComponent extends DefaultComponent implements ContributionPersistenceManager { 036 037 public static final String STORAGE_XP = "storage"; 038 039 protected ContributionStorage storage; 040 041 protected RuntimeContext ctx; 042 043 public static String getComponentName(String contribName) { 044 return "config:" + contribName; 045 } 046 047 @Override 048 public void activate(ComponentContext context) { 049 super.activate(context); 050 this.ctx = context.getRuntimeContext(); 051 storage = new FileSystemStorage(); 052 } 053 054 @Override 055 public void deactivate(ComponentContext context) { 056 super.deactivate(context); 057 ctx = null; 058 storage = null; 059 } 060 061 @Override 062 public void registerContribution(Object contribution, String extensionPoint, ComponentInstance contributor) { 063 // This extension point is a singleton. It supports only one 064 // contribution! 065 // I am not using a runtime property to specify the implementation class 066 // because 067 // of possible problems caused by class loaders in real OSGI frameworks. 068 ContributionStorageDescriptor c = (ContributionStorageDescriptor) contribution; 069 try { 070 storage = (ContributionStorage) c.clazz.newInstance(); 071 } catch (ReflectiveOperationException e) { 072 throw new RuntimeServiceException(e); 073 } 074 } 075 076 @Override 077 public void unregisterContribution(Object contribution, String extensionPoint, ComponentInstance contributor) { 078 storage = null; 079 } 080 081 @Override 082 public List<Contribution> getContributions() { 083 return storage.getContributions(); 084 } 085 086 @Override 087 public Contribution getContribution(String name) { 088 return storage.getContribution(name); 089 } 090 091 @Override 092 public Contribution addContribution(Contribution contrib) { 093 return storage.addContribution(contrib); 094 } 095 096 @Override 097 public boolean removeContribution(Contribution contrib) { 098 return storage.removeContribution(contrib); 099 } 100 101 @Override 102 public boolean isInstalled(Contribution contrib) { 103 return ctx.isDeployed(contrib); 104 } 105 106 @Override 107 public synchronized boolean installContribution(Contribution contrib) { 108 RegistrationInfo ri; 109 try { 110 ri = ctx.deploy(contrib); 111 } catch (IOException e) { 112 throw new RuntimeServiceException(e); 113 } 114 if (ri == null) { 115 return false; 116 } 117 ri.setPersistent(true); 118 return true; 119 } 120 121 @Override 122 public boolean uninstallContribution(Contribution contrib) { 123 boolean ret = isInstalled(contrib); 124 try { 125 ctx.undeploy(contrib); 126 } catch (IOException e) { 127 throw new RuntimeServiceException(e); 128 } 129 return ret; 130 } 131 132 @Override 133 public Contribution updateContribution(Contribution contribution) { 134 return storage.updateContribution(contribution); 135 } 136 137 @Override 138 public boolean isPersisted(Contribution contrib) { 139 return storage.getContribution(contrib.getName()) != null; 140 } 141 142 @Override 143 public void start() { 144 for (Contribution c : storage.getContributions()) { 145 if (!c.isDisabled()) { 146 installContribution(c); 147 } 148 } 149 } 150 151 @Override 152 public void stop() { 153 for (Contribution c : storage.getContributions()) { 154 if (!c.isDisabled()) { 155 uninstallContribution(c); 156 } 157 } 158 } 159 160 @Override 161 public void start(ComponentContext context) { 162 if (storage == null) { 163 storage = new FileSystemStorage(); 164 start(); 165 } 166 } 167 168 @Override 169 public void stop(ComponentContext context) { 170 stop(); 171 } 172 173}