Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 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 | */ |
| 16 | package org.onosproject.ui; |
| 17 | |
Thomas Vachuska | c4178cc | 2015-12-10 11:43:32 -0800 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableList; |
Thomas Vachuska | 3ccb9eb | 2015-04-29 23:33:56 -0700 | [diff] [blame] | 19 | import org.slf4j.Logger; |
| 20 | import org.slf4j.LoggerFactory; |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 21 | |
| 22 | import java.io.InputStream; |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 23 | import java.util.ArrayList; |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 24 | import java.util.List; |
| 25 | |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 26 | import static com.google.common.base.Preconditions.checkArgument; |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 27 | import static com.google.common.base.Preconditions.checkNotNull; |
| 28 | |
| 29 | /** |
Simon Hunt | 8add9ee | 2016-09-20 17:05:07 -0700 | [diff] [blame] | 30 | * Immutable representation of a user interface extension. |
| 31 | * <p> |
| 32 | * Note that the {@link Builder} class is used to create a user |
| 33 | * interface extension instance, and that these instances are immutable. |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 34 | */ |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 35 | public final class UiExtension { |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 36 | |
Thomas Vachuska | 3ccb9eb | 2015-04-29 23:33:56 -0700 | [diff] [blame] | 37 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 38 | |
Thomas Vachuska | 529db0a | 2015-02-23 16:42:14 -0800 | [diff] [blame] | 39 | private static final String VIEW_PREFIX = "app/view/"; |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 40 | private static final String EMPTY = ""; |
| 41 | private static final String SLASH = "/"; |
| 42 | private static final String CSS_HTML = "css.html"; |
| 43 | private static final String JS_HTML = "js.html"; |
Thomas Vachuska | 529db0a | 2015-02-23 16:42:14 -0800 | [diff] [blame] | 44 | |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 45 | private final ClassLoader classLoader; |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 46 | private final String resourcePath; |
Simon Hunt | 8add9ee | 2016-09-20 17:05:07 -0700 | [diff] [blame] | 47 | private final List<UiView> viewList; |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 48 | private final UiMessageHandlerFactory messageHandlerFactory; |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 49 | private final UiTopoOverlayFactory topoOverlayFactory; |
Steven Burrows | 3a9a644 | 2016-05-05 15:31:16 +0100 | [diff] [blame] | 50 | private final UiTopoMapFactory topoMapFactory; |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 51 | |
Thomas Vachuska | c4178cc | 2015-12-10 11:43:32 -0800 | [diff] [blame] | 52 | private boolean isValid = true; |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 53 | |
| 54 | // private constructor - only the builder calls this |
| 55 | private UiExtension(ClassLoader cl, String path, List<UiView> views, |
| 56 | UiMessageHandlerFactory mhFactory, |
Steven Burrows | 3a9a644 | 2016-05-05 15:31:16 +0100 | [diff] [blame] | 57 | UiTopoOverlayFactory toFactory, |
| 58 | UiTopoMapFactory tmFactory) { |
Simon Hunt | 8add9ee | 2016-09-20 17:05:07 -0700 | [diff] [blame] | 59 | classLoader = cl; |
| 60 | resourcePath = path; |
| 61 | viewList = views; |
| 62 | messageHandlerFactory = mhFactory; |
| 63 | topoOverlayFactory = toFactory; |
| 64 | topoMapFactory = tmFactory; |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 65 | } |
| 66 | |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 67 | |
| 68 | /** |
| 69 | * Returns input stream containing CSS inclusion statements. |
| 70 | * |
| 71 | * @return CSS inclusion statements |
| 72 | */ |
| 73 | public InputStream css() { |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 74 | return getStream(resourcePath + CSS_HTML); |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Returns input stream containing JavaScript inclusion statements. |
| 79 | * |
| 80 | * @return JavaScript inclusion statements |
| 81 | */ |
| 82 | public InputStream js() { |
Thomas Vachuska | d894b5d | 2015-07-30 11:59:07 -0700 | [diff] [blame] | 83 | return getStream(resourcePath + JS_HTML); |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Returns list of user interface views contributed by this extension. |
| 88 | * |
| 89 | * @return contributed view descriptors |
| 90 | */ |
| 91 | public List<UiView> views() { |
Simon Hunt | 8add9ee | 2016-09-20 17:05:07 -0700 | [diff] [blame] | 92 | return isValid ? viewList : ImmutableList.of(); |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Returns input stream containing specified view-specific resource. |
| 97 | * |
| 98 | * @param viewId view identifier |
| 99 | * @param path resource path, relative to the view directory |
| 100 | * @return resource input stream |
| 101 | */ |
| 102 | public InputStream resource(String viewId, String path) { |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 103 | return getStream(VIEW_PREFIX + viewId + SLASH + path); |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 104 | } |
| 105 | |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 106 | /** |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 107 | * Returns message handler factory, if one was defined. |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 108 | * |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 109 | * @return message handler factory |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 110 | */ |
| 111 | public UiMessageHandlerFactory messageHandlerFactory() { |
| 112 | return messageHandlerFactory; |
| 113 | } |
Thomas Vachuska | 3ccb9eb | 2015-04-29 23:33:56 -0700 | [diff] [blame] | 114 | |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 115 | /** |
| 116 | * Returns the topology overlay factory, if one was defined. |
| 117 | * |
| 118 | * @return topology overlay factory |
| 119 | */ |
| 120 | public UiTopoOverlayFactory topoOverlayFactory() { |
| 121 | return topoOverlayFactory; |
| 122 | } |
| 123 | |
Steven Burrows | 3a9a644 | 2016-05-05 15:31:16 +0100 | [diff] [blame] | 124 | /** |
| 125 | * Returns the topology map factory, if one was defined. |
| 126 | * |
| 127 | * @return topology map factory |
| 128 | */ |
| 129 | public UiTopoMapFactory topoMapFactory() { |
| 130 | return topoMapFactory; |
| 131 | } |
| 132 | |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 133 | |
Thomas Vachuska | 3ccb9eb | 2015-04-29 23:33:56 -0700 | [diff] [blame] | 134 | // Returns the resource input stream from the specified class-loader. |
| 135 | private InputStream getStream(String path) { |
| 136 | InputStream stream = classLoader.getResourceAsStream(path); |
| 137 | if (stream == null) { |
Thomas Vachuska | c4178cc | 2015-12-10 11:43:32 -0800 | [diff] [blame] | 138 | isValid = false; |
Thomas Vachuska | 3ccb9eb | 2015-04-29 23:33:56 -0700 | [diff] [blame] | 139 | log.warn("Unable to find resource {}", path); |
| 140 | } |
| 141 | return stream; |
| 142 | } |
| 143 | |
| 144 | |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 145 | /** |
| 146 | * UI Extension Builder. |
| 147 | */ |
| 148 | public static class Builder { |
| 149 | private ClassLoader classLoader; |
| 150 | |
| 151 | private String resourcePath = EMPTY; |
Simon Hunt | 8add9ee | 2016-09-20 17:05:07 -0700 | [diff] [blame] | 152 | private List<UiView> viewList = new ArrayList<>(); |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 153 | private UiMessageHandlerFactory messageHandlerFactory = null; |
| 154 | private UiTopoOverlayFactory topoOverlayFactory = null; |
Steven Burrows | 3a9a644 | 2016-05-05 15:31:16 +0100 | [diff] [blame] | 155 | private UiTopoMapFactory topoMapFactory = null; |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 156 | |
| 157 | /** |
| 158 | * Create a builder with the given class loader. |
| 159 | * Resource path defaults to "". |
| 160 | * Views defaults to an empty list. |
Simon Hunt | 8add9ee | 2016-09-20 17:05:07 -0700 | [diff] [blame] | 161 | * MessageHandler, TopoOverlay, and TopoMap factories default to null. |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 162 | * |
Thomas Vachuska | d894b5d | 2015-07-30 11:59:07 -0700 | [diff] [blame] | 163 | * @param cl the class loader |
| 164 | * @param views list of views contributed by this extension |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 165 | */ |
| 166 | public Builder(ClassLoader cl, List<UiView> views) { |
| 167 | checkNotNull(cl, "Must provide a class loader"); |
Jon Hall | cbd1b39 | 2017-01-18 20:15:44 -0800 | [diff] [blame] | 168 | checkArgument(!views.isEmpty(), "Must provide at least one view"); |
Simon Hunt | 8add9ee | 2016-09-20 17:05:07 -0700 | [diff] [blame] | 169 | classLoader = cl; |
| 170 | viewList = views; |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | /** |
Simon Hunt | 8add9ee | 2016-09-20 17:05:07 -0700 | [diff] [blame] | 174 | * Set the resource path. That is, the path to where the CSS and JS |
| 175 | * files are located. |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 176 | * |
| 177 | * @param path resource path |
| 178 | * @return self, for chaining |
| 179 | */ |
| 180 | public Builder resourcePath(String path) { |
Simon Hunt | 8add9ee | 2016-09-20 17:05:07 -0700 | [diff] [blame] | 181 | resourcePath = path == null ? EMPTY : path + SLASH; |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 182 | return this; |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Sets the message handler factory for this extension. |
| 187 | * |
| 188 | * @param mhFactory message handler factory |
| 189 | * @return self, for chaining |
| 190 | */ |
| 191 | public Builder messageHandlerFactory(UiMessageHandlerFactory mhFactory) { |
Simon Hunt | 8add9ee | 2016-09-20 17:05:07 -0700 | [diff] [blame] | 192 | messageHandlerFactory = mhFactory; |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 193 | return this; |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Sets the topology overlay factory for this extension. |
| 198 | * |
| 199 | * @param toFactory topology overlay factory |
| 200 | * @return self, for chaining |
| 201 | */ |
| 202 | public Builder topoOverlayFactory(UiTopoOverlayFactory toFactory) { |
Simon Hunt | 8add9ee | 2016-09-20 17:05:07 -0700 | [diff] [blame] | 203 | topoOverlayFactory = toFactory; |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 204 | return this; |
| 205 | } |
| 206 | |
| 207 | /** |
Steven Burrows | 3a9a644 | 2016-05-05 15:31:16 +0100 | [diff] [blame] | 208 | * Sets the topology map factory for this extension. |
| 209 | * |
| 210 | * @param tmFactory topology map factory |
| 211 | * @return self, for chaining |
| 212 | */ |
| 213 | public Builder topoMapFactory(UiTopoMapFactory tmFactory) { |
Simon Hunt | 8add9ee | 2016-09-20 17:05:07 -0700 | [diff] [blame] | 214 | topoMapFactory = tmFactory; |
Steven Burrows | 3a9a644 | 2016-05-05 15:31:16 +0100 | [diff] [blame] | 215 | return this; |
| 216 | } |
| 217 | |
| 218 | /** |
Simon Hunt | 8add9ee | 2016-09-20 17:05:07 -0700 | [diff] [blame] | 219 | * Builds the user interface extension. |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 220 | * |
| 221 | * @return UI extension instance |
| 222 | */ |
| 223 | public UiExtension build() { |
Simon Hunt | 8add9ee | 2016-09-20 17:05:07 -0700 | [diff] [blame] | 224 | return new UiExtension(classLoader, resourcePath, viewList, |
Steven Burrows | 3a9a644 | 2016-05-05 15:31:16 +0100 | [diff] [blame] | 225 | messageHandlerFactory, topoOverlayFactory, |
| 226 | topoMapFactory); |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 227 | } |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 228 | } |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 229 | } |