001/*
002 * Copyright 2013 Atteo.
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 */
016package org.atteo.moonshine.services.internal;
017
018import java.lang.annotation.Annotation;
019import java.util.ArrayList;
020import java.util.List;
021
022import org.atteo.evo.classindex.ClassIndex;
023import org.atteo.moonshine.services.Service;
024import org.atteo.moonshine.services.ServiceInfo;
025
026import com.google.inject.spi.Element;
027
028public class ServiceMetadata implements ServiceInfo {
029    private final String name;
030    private final Service service;
031    private final List<Dependency> dependencies = new ArrayList<>();
032    private Status status = Status.CREATED;
033    private List<com.google.inject.spi.Element> elements;
034    private boolean singleton = false;
035
036    public ServiceMetadata(Service service) {
037        this.service = service;
038        this.name = getServiceName(service);
039    }
040
041    public void addDependency(ServiceMetadata service, Annotation annotation) {
042        dependencies.add(new Dependency(service, annotation));
043    }
044
045    public List<Dependency> getDependencies() {
046        return dependencies;
047    }
048
049    public Service getService() {
050        return service;
051    }
052
053    @Override
054    public String getName() {
055        return name;
056    }
057
058    public void setElements(List<Element> elements) {
059        this.elements = elements;
060    }
061
062    public Status getStatus() {
063        return status;
064    }
065
066    public void setStatus(Status status) {
067        this.status = status;
068    }
069
070    @Override
071    public List<Element> getElements() {
072        return elements;
073    }
074
075    private static String getServiceName(Service service) {
076        StringBuilder builder = new StringBuilder();
077
078        if (service.getId() != null) {
079            builder.append("\"");
080            builder.append(service.getId());
081            builder.append("\" ");
082        }
083
084        String summary = ClassIndex.getClassSummary(service.getClass());
085        builder.append(service.getClass().getSimpleName());
086        if (summary != null) {
087            builder.append(" (");
088            builder.append(summary);
089            builder.append(")");
090        }
091        return builder.toString();
092    }
093
094    public boolean isSingleton() {
095        return singleton;
096    }
097
098    public void setSingleton(boolean singleton) {
099        this.singleton = singleton;
100    }
101
102    public static class Dependency {
103        private final ServiceMetadata service;
104        private final Annotation annotation;
105
106        public Dependency(ServiceMetadata service, Annotation annotation) {
107            this.service = service;
108            this.annotation = annotation;
109        }
110
111        public Annotation getAnnotation() {
112            return annotation;
113        }
114
115        public ServiceMetadata getService() {
116            return service;
117        }
118    }
119
120    public static enum Status {
121        CREATED, READY, STARTED, CLOSED
122    }
123}