blob: cc105069623fc57549fbef92411c798ef6743f5f [file] [log] [blame]
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -08001/*
2 * Copyright 2015 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.ui;
17
18import com.google.common.collect.ImmutableList;
Thomas Vachuska3ccb9eb2015-04-29 23:33:56 -070019import org.slf4j.Logger;
20import org.slf4j.LoggerFactory;
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080021
22import java.io.InputStream;
23import java.util.List;
24
25import static com.google.common.base.Preconditions.checkNotNull;
26
27/**
28 * User interface extension.
29 */
30public class UiExtension {
31
Thomas Vachuska3ccb9eb2015-04-29 23:33:56 -070032 private final Logger log = LoggerFactory.getLogger(getClass());
33
Thomas Vachuska529db0a2015-02-23 16:42:14 -080034 private static final String VIEW_PREFIX = "app/view/";
35
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080036 private final String prefix;
37 private final ClassLoader classLoader;
38 private final List<UiView> views;
Thomas Vachuska3553b302015-03-07 14:49:43 -080039 private final UiMessageHandlerFactory messageHandlerFactory;
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080040
41 /**
42 * Creates a user interface extension for loading CSS and JS injections
43 * from {@code css.html} and {@code js.html} resources, respectively.
44 *
Thomas Vachuska3553b302015-03-07 14:49:43 -080045 * @param views list of contributed views
46 * @param messageHandlerFactory optional message handler factory
47 * @param classLoader class-loader for user interface resources
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080048 */
Thomas Vachuska3553b302015-03-07 14:49:43 -080049 public UiExtension(List<UiView> views,
50 UiMessageHandlerFactory messageHandlerFactory,
51 ClassLoader classLoader) {
52 this(views, messageHandlerFactory, null, classLoader);
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080053 }
54
55 /**
56 * Creates a user interface extension using custom resource prefix. It
57 * loads CSS and JS injections from {@code path/css.html} and
58 * {@code prefix/js.html} resources, respectively.
59 *
Thomas Vachuska3553b302015-03-07 14:49:43 -080060 * @param views list of user interface views
61 * @param messageHandlerFactory optional message handler factory
62 * @param path resource path prefix
63 * @param classLoader class-loader for user interface resources
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080064 */
Thomas Vachuska3553b302015-03-07 14:49:43 -080065 public UiExtension(List<UiView> views,
66 UiMessageHandlerFactory messageHandlerFactory,
67 String path, ClassLoader classLoader) {
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080068 this.views = checkNotNull(ImmutableList.copyOf(views), "Views cannot be null");
Thomas Vachuska3553b302015-03-07 14:49:43 -080069 this.messageHandlerFactory = messageHandlerFactory;
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080070 this.prefix = path != null ? (path + "/") : "";
71 this.classLoader = checkNotNull(classLoader, "Class loader must be specified");
72 }
73
74 /**
75 * Returns input stream containing CSS inclusion statements.
76 *
77 * @return CSS inclusion statements
78 */
79 public InputStream css() {
Thomas Vachuska3ccb9eb2015-04-29 23:33:56 -070080 return getStream(prefix + "css.html");
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080081 }
82
83 /**
84 * Returns input stream containing JavaScript inclusion statements.
85 *
86 * @return JavaScript inclusion statements
87 */
88 public InputStream js() {
Thomas Vachuska3ccb9eb2015-04-29 23:33:56 -070089 return getStream(prefix + "js.html");
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080090 }
91
92 /**
93 * Returns list of user interface views contributed by this extension.
94 *
95 * @return contributed view descriptors
96 */
97 public List<UiView> views() {
98 return views;
99 }
100
101 /**
102 * Returns input stream containing specified view-specific resource.
103 *
104 * @param viewId view identifier
105 * @param path resource path, relative to the view directory
106 * @return resource input stream
107 */
108 public InputStream resource(String viewId, String path) {
Thomas Vachuska3ccb9eb2015-04-29 23:33:56 -0700109 return getStream(VIEW_PREFIX + viewId + "/" + path);
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -0800110 }
111
Thomas Vachuska3553b302015-03-07 14:49:43 -0800112 /**
113 * Returns message handler factory.
114 *
115 * @return message handlers
116 */
117 public UiMessageHandlerFactory messageHandlerFactory() {
118 return messageHandlerFactory;
119 }
Thomas Vachuska3ccb9eb2015-04-29 23:33:56 -0700120
121 // Returns the resource input stream from the specified class-loader.
122 private InputStream getStream(String path) {
123 InputStream stream = classLoader.getResourceAsStream(path);
124 if (stream == null) {
125 log.warn("Unable to find resource {}", path);
126 }
127 return stream;
128 }
129
130
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -0800131}