Java tutorial
/* * Copyright 2015 52North Initiative for Geospatial Open Source * Software GmbH * * 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 org.n52.iceland.binding; import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; import java.util.Set; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.n52.iceland.component.AbstractComponentRepository; import org.n52.iceland.lifecycle.Constructable; import org.n52.iceland.util.Producer; import org.n52.iceland.util.Producers; import org.n52.iceland.util.activation.Activatables; import org.n52.iceland.util.activation.ActivationListener; import org.n52.iceland.util.activation.ActivationListeners; import org.n52.iceland.util.activation.ActivationManager; import org.n52.iceland.util.activation.ActivationSource; import org.n52.iceland.util.http.MediaType; import com.google.common.collect.Maps; /** * @author <a href="mailto:c.autermann@52north.org">Christian Autermann</a> * * @since 1.0.0 */ public class BindingRepository extends AbstractComponentRepository<BindingKey, Binding, BindingFactory> implements ActivationManager<BindingKey>, ActivationSource<BindingKey>, Constructable { private static final Logger LOG = LoggerFactory.getLogger(BindingRepository.class); @Deprecated private static BindingRepository instance; private final ActivationListeners<BindingKey> activation = new ActivationListeners<>(true); private final Map<PathBindingKey, Producer<Binding>> byPath = Maps.newHashMap(); private final Map<MediaTypeBindingKey, Producer<Binding>> byMediaType = Maps.newHashMap(); private final Map<BindingKey, Producer<Binding>> bindings = Maps.newHashMap(); private Collection<Binding> components; private Collection<BindingFactory> componentFactories; @Autowired(required = false) public void setComponentFactories(Collection<BindingFactory> componentFactories) { this.componentFactories = componentFactories; } @Autowired(required = false) public void setComponents(Collection<Binding> components) { this.components = components; } @Override public void registerListener(ActivationListener<BindingKey> listener) { this.activation.registerListener(listener); } @Override public void deregisterListener(ActivationListener<BindingKey> listener) { this.activation.deregisterListener(listener); } @Override public boolean isActive(BindingKey key) { return this.activation.isActive(key); } @Override public void activate(BindingKey key) { this.activation.activate(key); } @Override public void deactivate(BindingKey key) { this.activation.deactivate(key); } @Override public Set<BindingKey> getKeys() { return Collections.unmodifiableSet(this.bindings.keySet()); } @Override public void setActive(BindingKey bk, boolean active) { this.activation.setActive(bk, active); } @Override public void init() { BindingRepository.instance = this; Map<BindingKey, Producer<Binding>> implementations = getUniqueProviders(this.components, this.componentFactories); this.bindings.clear(); this.byMediaType.clear(); this.byPath.clear(); for (Entry<BindingKey, Producer<Binding>> entry : implementations.entrySet()) { BindingKey key = entry.getKey(); Producer<Binding> binding = entry.getValue(); this.bindings.put(key, binding); if (key instanceof MediaTypeBindingKey) { byMediaType.put((MediaTypeBindingKey) key, binding); } else if (key instanceof PathBindingKey) { byPath.put((PathBindingKey) key, binding); } } if (this.bindings.isEmpty()) { final StringBuilder exceptionText = new StringBuilder(); exceptionText.append("No Binding implementation could be loaded! "); exceptionText.append("If the service is not used as a webapp, this has no effect! "); exceptionText.append("Please add a Binding implementation!"); LOG.warn(exceptionText.toString()); } } public Binding getBinding(String urlPattern) { return getBinding(new PathBindingKey(urlPattern)); } public Binding getBinding(MediaType mediaType) { return getBinding(new MediaTypeBindingKey(mediaType)); } public Binding getBinding(BindingKey key) { Producer<Binding> binding = this.bindings.get(key); return binding == null ? null : binding.get(); } public boolean isBindingSupported(String urlPattern) { return isActive(new PathBindingKey(urlPattern)); } public boolean isBindingSupported(MediaType mediaType) { return isActive(new MediaTypeBindingKey(mediaType)); } public boolean isBindingSupported(BindingKey key) { return isActive(key); } public boolean isActive(String urlPattern) { return isActive(new PathBindingKey(urlPattern)); } public boolean isActive(MediaType mediaType) { return isActive(new MediaTypeBindingKey(mediaType)); } public Map<BindingKey, Binding> getBindings() { Map<BindingKey, Producer<Binding>> actives = Activatables.activatedMap(this.bindings, this.activation); return Producers.produce(actives); } public Map<String, Binding> getBindingsByPath() { Map<String, Binding> map = new HashMap<>(this.byPath.size()); for (Entry<PathBindingKey, Producer<Binding>> entry : this.byPath.entrySet()) { PathBindingKey key = entry.getKey(); Producer<Binding> producer = entry.getValue(); if (isActive(key)) { map.put(key.getPath(), producer.get()); } } return map; } public Map<MediaType, Binding> getBindingsByMediaType() { Map<MediaType, Binding> map = new HashMap<>(this.byPath.size()); for (Entry<MediaTypeBindingKey, Producer<Binding>> entry : this.byMediaType.entrySet()) { MediaTypeBindingKey key = entry.getKey(); Producer<Binding> producer = entry.getValue(); if (isActive(key)) { map.put(key.getMediaType(), producer.get()); } } return map; } public Map<String, Binding> getAllBindingsByPath() { Map<String, Binding> map = new HashMap<>(this.byPath.size()); for (Entry<PathBindingKey, Producer<Binding>> entry : this.byPath.entrySet()) { PathBindingKey key = entry.getKey(); Producer<Binding> producer = entry.getValue(); map.put(key.getPath(), producer.get()); } return map; } public Map<MediaType, Binding> getAllBindingsByMediaType() { Map<MediaType, Binding> map = new HashMap<>(this.byPath.size()); for (Entry<MediaTypeBindingKey, Producer<Binding>> entry : this.byMediaType.entrySet()) { MediaTypeBindingKey key = entry.getKey(); Producer<Binding> producer = entry.getValue(); map.put(key.getMediaType(), producer.get()); } return map; } @Deprecated public static BindingRepository getInstance() { return BindingRepository.instance; } }