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