001/* 002 * (C) Copyright 2006-2018 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 * Nuxeo - initial API and implementation 018 */ 019 020package org.nuxeo.runtime.model; 021 022import static java.util.Objects.requireNonNull; 023 024import java.util.List; 025 026import org.nuxeo.runtime.model.impl.ComponentManagerImpl; 027 028/** 029 * Empty implementation for a component. 030 * 031 * @author <a href="mailto:[email protected]">Bogdan Stefanescu</a> 032 */ 033public class DefaultComponent implements Component, Adaptable { 034 035 /** @since 10.3 */ 036 protected String name; 037 038 /** 039 * @since 5.6 040 */ 041 protected Long lastModified; 042 043 private DescriptorRegistry registry; 044 045 @Override 046 public void setName(String name) { 047 this.name = requireNonNull(name); 048 } 049 050 @Override 051 public void activate(ComponentContext context) { 052 registry = ((ComponentManagerImpl) context.getRuntimeContext() 053 .getRuntime() 054 .getComponentManager()).getDescriptors(); 055 setModifiedNow(); 056 } 057 058 @Override 059 public void deactivate(ComponentContext context) { 060 if (getRegistry() != null) { 061 getRegistry().clear(); 062 } 063 setModifiedNow(); 064 } 065 066 @Override 067 public void registerExtension(Extension extension) { 068 Object[] contribs = extension.getContributions(); 069 if (contribs == null) { 070 return; 071 } 072 for (Object contrib : contribs) { 073 registerContribution(contrib, extension.getExtensionPoint(), extension.getComponent()); 074 } 075 setModifiedNow(); 076 } 077 078 @Override 079 public void unregisterExtension(Extension extension) { 080 Object[] contribs = extension.getContributions(); 081 if (contribs == null) { 082 return; 083 } 084 for (Object contrib : contribs) { 085 unregisterContribution(contrib, extension.getExtensionPoint(), extension.getComponent()); 086 } 087 setModifiedNow(); 088 } 089 090 public void registerContribution(Object contribution, String xp, ComponentInstance component) { 091 if (contribution instanceof Descriptor) { 092 register(xp, (Descriptor) contribution); 093 } 094 } 095 096 public void unregisterContribution(Object contribution, String xp, ComponentInstance component) { 097 if (contribution instanceof Descriptor) { 098 unregister(xp, (Descriptor) contribution); 099 } 100 } 101 102 @Override 103 public <T> T getAdapter(Class<T> adapter) { 104 return adapter.cast(this); 105 } 106 107 @Override 108 public void start(ComponentContext context) { 109 // delegate for now to applicationStarted 110 applicationStarted(context); 111 } 112 113 @Override 114 public void stop(ComponentContext context) throws InterruptedException { 115 } 116 117 /** 118 * Sets the last modified date to current date timestamp 119 * 120 * @since 5.6 121 */ 122 protected void setModifiedNow() { 123 setLastModified(Long.valueOf(System.currentTimeMillis())); 124 } 125 126 @Override 127 public Long getLastModified() { 128 return lastModified; 129 } 130 131 @Override 132 public void setLastModified(Long lastModified) { 133 this.lastModified = lastModified; 134 } 135 136 /** 137 * @since 10.3 138 */ 139 protected DescriptorRegistry getRegistry() { 140 return registry; 141 } 142 143 /** 144 * @since 10.3 145 */ 146 protected boolean register(String xp, Descriptor descriptor) { 147 return getRegistry().register(name, xp, descriptor); 148 } 149 150 /** 151 * @since 10.3 152 */ 153 protected boolean unregister(String xp, Descriptor descriptor) { 154 return getRegistry().unregister(name, xp, descriptor); 155 } 156 157 /** 158 * @since 10.3 159 */ 160 protected <T extends Descriptor> T getDescriptor(String xp, String id) { 161 return getRegistry().getDescriptor(name, xp, id); 162 } 163 164 /** 165 * @since 10.3 166 */ 167 protected <T extends Descriptor> List<T> getDescriptors(String xp) { 168 return getRegistry().getDescriptors(name, xp); 169 } 170 171}