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 | /** |
| 30 | * User interface extension. |
| 31 | */ |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 32 | public final class UiExtension { |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 33 | |
Thomas Vachuska | 3ccb9eb | 2015-04-29 23:33:56 -0700 | [diff] [blame] | 34 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 35 | |
Thomas Vachuska | 529db0a | 2015-02-23 16:42:14 -0800 | [diff] [blame] | 36 | private static final String VIEW_PREFIX = "app/view/"; |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 37 | private static final String EMPTY = ""; |
| 38 | private static final String SLASH = "/"; |
| 39 | private static final String CSS_HTML = "css.html"; |
| 40 | private static final String JS_HTML = "js.html"; |
Thomas Vachuska | 529db0a | 2015-02-23 16:42:14 -0800 | [diff] [blame] | 41 | |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 42 | private final ClassLoader classLoader; |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 43 | private final String resourcePath; |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 44 | private final List<UiView> views; |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 45 | private final UiMessageHandlerFactory messageHandlerFactory; |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 46 | private final UiTopoOverlayFactory topoOverlayFactory; |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 47 | |
Thomas Vachuska | c4178cc | 2015-12-10 11:43:32 -0800 | [diff] [blame] | 48 | private boolean isValid = true; |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 49 | |
| 50 | // private constructor - only the builder calls this |
| 51 | private UiExtension(ClassLoader cl, String path, List<UiView> views, |
| 52 | UiMessageHandlerFactory mhFactory, |
| 53 | UiTopoOverlayFactory toFactory) { |
| 54 | this.classLoader = cl; |
| 55 | this.resourcePath = path; |
| 56 | this.views = views; |
| 57 | this.messageHandlerFactory = mhFactory; |
| 58 | this.topoOverlayFactory = toFactory; |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 59 | } |
| 60 | |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 61 | |
| 62 | /** |
| 63 | * Returns input stream containing CSS inclusion statements. |
| 64 | * |
| 65 | * @return CSS inclusion statements |
| 66 | */ |
| 67 | public InputStream css() { |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 68 | return getStream(resourcePath + CSS_HTML); |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Returns input stream containing JavaScript inclusion statements. |
| 73 | * |
| 74 | * @return JavaScript inclusion statements |
| 75 | */ |
| 76 | public InputStream js() { |
Thomas Vachuska | d894b5d | 2015-07-30 11:59:07 -0700 | [diff] [blame] | 77 | return getStream(resourcePath + JS_HTML); |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Returns list of user interface views contributed by this extension. |
| 82 | * |
| 83 | * @return contributed view descriptors |
| 84 | */ |
| 85 | public List<UiView> views() { |
Thomas Vachuska | c4178cc | 2015-12-10 11:43:32 -0800 | [diff] [blame] | 86 | return isValid ? views : ImmutableList.of(); |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Returns input stream containing specified view-specific resource. |
| 91 | * |
| 92 | * @param viewId view identifier |
| 93 | * @param path resource path, relative to the view directory |
| 94 | * @return resource input stream |
| 95 | */ |
| 96 | public InputStream resource(String viewId, String path) { |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 97 | return getStream(VIEW_PREFIX + viewId + SLASH + path); |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 98 | } |
| 99 | |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 100 | /** |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 101 | * Returns message handler factory, if one was defined. |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 102 | * |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 103 | * @return message handler factory |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 104 | */ |
| 105 | public UiMessageHandlerFactory messageHandlerFactory() { |
| 106 | return messageHandlerFactory; |
| 107 | } |
Thomas Vachuska | 3ccb9eb | 2015-04-29 23:33:56 -0700 | [diff] [blame] | 108 | |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 109 | /** |
| 110 | * Returns the topology overlay factory, if one was defined. |
| 111 | * |
| 112 | * @return topology overlay factory |
| 113 | */ |
| 114 | public UiTopoOverlayFactory topoOverlayFactory() { |
| 115 | return topoOverlayFactory; |
| 116 | } |
| 117 | |
| 118 | |
Thomas Vachuska | 3ccb9eb | 2015-04-29 23:33:56 -0700 | [diff] [blame] | 119 | // Returns the resource input stream from the specified class-loader. |
| 120 | private InputStream getStream(String path) { |
| 121 | InputStream stream = classLoader.getResourceAsStream(path); |
| 122 | if (stream == null) { |
Thomas Vachuska | c4178cc | 2015-12-10 11:43:32 -0800 | [diff] [blame] | 123 | isValid = false; |
Thomas Vachuska | 3ccb9eb | 2015-04-29 23:33:56 -0700 | [diff] [blame] | 124 | log.warn("Unable to find resource {}", path); |
| 125 | } |
| 126 | return stream; |
| 127 | } |
| 128 | |
| 129 | |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 130 | /** |
| 131 | * UI Extension Builder. |
| 132 | */ |
| 133 | public static class Builder { |
| 134 | private ClassLoader classLoader; |
| 135 | |
| 136 | private String resourcePath = EMPTY; |
| 137 | private List<UiView> views = new ArrayList<>(); |
| 138 | private UiMessageHandlerFactory messageHandlerFactory = null; |
| 139 | private UiTopoOverlayFactory topoOverlayFactory = null; |
| 140 | |
| 141 | /** |
| 142 | * Create a builder with the given class loader. |
| 143 | * Resource path defaults to "". |
| 144 | * Views defaults to an empty list. |
| 145 | * Both Message and TopoOverlay factories default to null. |
| 146 | * |
Thomas Vachuska | d894b5d | 2015-07-30 11:59:07 -0700 | [diff] [blame] | 147 | * @param cl the class loader |
| 148 | * @param views list of views contributed by this extension |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 149 | */ |
| 150 | public Builder(ClassLoader cl, List<UiView> views) { |
| 151 | checkNotNull(cl, "Must provide a class loader"); |
| 152 | checkArgument(views.size() > 0, "Must provide at least one view"); |
| 153 | this.classLoader = cl; |
| 154 | this.views = views; |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * Set the resource path. That is, path to where the CSS and JS |
| 159 | * files are located. This value should |
| 160 | * |
| 161 | * @param path resource path |
| 162 | * @return self, for chaining |
| 163 | */ |
| 164 | public Builder resourcePath(String path) { |
| 165 | this.resourcePath = path == null ? EMPTY : path + SLASH; |
| 166 | return this; |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Sets the message handler factory for this extension. |
| 171 | * |
| 172 | * @param mhFactory message handler factory |
| 173 | * @return self, for chaining |
| 174 | */ |
| 175 | public Builder messageHandlerFactory(UiMessageHandlerFactory mhFactory) { |
| 176 | this.messageHandlerFactory = mhFactory; |
| 177 | return this; |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Sets the topology overlay factory for this extension. |
| 182 | * |
| 183 | * @param toFactory topology overlay factory |
| 184 | * @return self, for chaining |
| 185 | */ |
| 186 | public Builder topoOverlayFactory(UiTopoOverlayFactory toFactory) { |
| 187 | this.topoOverlayFactory = toFactory; |
| 188 | return this; |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * Builds the UI extension. |
| 193 | * |
| 194 | * @return UI extension instance |
| 195 | */ |
| 196 | public UiExtension build() { |
| 197 | return new UiExtension(classLoader, resourcePath, views, |
| 198 | messageHandlerFactory, topoOverlayFactory); |
| 199 | } |
| 200 | |
| 201 | } |
| 202 | |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 203 | } |