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