|
| 1 | +/*- |
| 2 | + * #%L |
| 3 | + * Json Migration Helper |
| 4 | + * %% |
| 5 | + * Copyright (C) 2025 Flowing Code |
| 6 | + * %% |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + * you may not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + * #L% |
| 19 | + */ |
| 20 | +package com.flowingcode.vaadin.jsonmigration; |
| 21 | + |
| 22 | +import com.vaadin.flow.component.Component; |
| 23 | +import com.vaadin.flow.router.RouteConfiguration; |
| 24 | +import com.vaadin.flow.server.VaadinServiceInitListener; |
| 25 | +import com.vaadin.flow.server.Version; |
| 26 | + |
| 27 | +/** |
| 28 | + * Abstract base class for Vaadin service initializers that register instrumented views. Subclasses |
| 29 | + * should implement {@link #serviceInit(com.vaadin.flow.server.ServiceInitEvent)} and call |
| 30 | + * {@link #registerInstrumentedRoute(Class)} to register views with instrumented routes. |
| 31 | + * |
| 32 | + * @author Javier Godoy / Flowing Code |
| 33 | + */ |
| 34 | +@SuppressWarnings("serial") |
| 35 | +public abstract class InstrumentationViewInitializer implements VaadinServiceInitListener { |
| 36 | + |
| 37 | + /** |
| 38 | + * Registers an instrumented route for the given navigation target. The navigation target must be |
| 39 | + * annotated with {@link InstrumentedRoute} to specify the route path. This method calls |
| 40 | + * {@link JsonMigration#instrumentClass(Class)} to get the instrumented class and registers it as |
| 41 | + * a Vaadin view with the route derived from the annotation. |
| 42 | + * |
| 43 | + * @param navigationTarget the component class to instrument and register, must be annotated with |
| 44 | + * {@link InstrumentedRoute} |
| 45 | + * @throws IllegalArgumentException if the navigationTarget is not annotated with |
| 46 | + * {@link InstrumentedRoute} |
| 47 | + */ |
| 48 | + protected final void registerInstrumentedRoute(Class<? extends Component> navigationTarget) { |
| 49 | + InstrumentedRoute annotation = navigationTarget.getAnnotation(InstrumentedRoute.class); |
| 50 | + if (annotation == null) { |
| 51 | + throw new IllegalArgumentException( |
| 52 | + navigationTarget.getName() + " must be annotated with @" |
| 53 | + + InstrumentedRoute.class.getSimpleName()); |
| 54 | + } |
| 55 | + |
| 56 | + String route = annotation.value(); |
| 57 | + if (Version.getMajorVersion() > 24) { |
| 58 | + navigationTarget = JsonMigration.instrumentClass(navigationTarget); |
| 59 | + } |
| 60 | + RouteConfiguration.forApplicationScope().setRoute(route, navigationTarget); |
| 61 | + } |
| 62 | + |
| 63 | +} |
0 commit comments