88 ******************************************************************************/
99
1010/******************************************************************************
11- * Copyright (c) 2018-2022 Payara Foundation
11+ * Copyright (c) 2018-2026 Payara Foundation
1212 * All rights reserved. This program and the accompanying materials
1313 * are made available under the terms of the Eclipse Public License v2.0
1414 * which accompanies this distribution, and is available at
2323import static java .nio .charset .Charset .defaultCharset ;
2424import static org .eclipse .core .runtime .IStatus .ERROR ;
2525import static org .eclipse .core .runtime .IStatus .INFO ;
26- import static org .eclipse .jface .resource .ImageDescriptor .createFromURL ;
2726import static org .eclipse .wst .server .core .ServerCore .addRuntimeLifecycleListener ;
2827
28+ import java .io .ByteArrayInputStream ;
2929import java .io .BufferedReader ;
30+ import java .io .IOException ;
31+ import java .io .InputStream ;
3032import java .io .InputStreamReader ;
3133import java .util .Arrays ;
3234import java .util .HashMap ;
3941import org .eclipse .core .runtime .Status ;
4042import org .eclipse .jface .resource .ImageDescriptor ;
4143import org .eclipse .jface .resource .ImageRegistry ;
44+ import org .eclipse .swt .SWTException ;
4245import org .eclipse .swt .graphics .Image ;
46+ import org .eclipse .swt .graphics .ImageData ;
4347import org .eclipse .ui .plugin .AbstractUIPlugin ;
4448import org .eclipse .wst .server .core .IRuntime ;
4549import org .eclipse .wst .server .core .internal .ResourceManager ;
@@ -89,18 +93,37 @@ public void start(BundleContext context) throws Exception {
8993 @ Override
9094 protected void initializeImageRegistry (ImageRegistry reg ) {
9195 super .initializeImageRegistry (reg );
92- reg .put (GF_SERVER_IMG , createFromURL ( getBundle (). getEntry ( "icons/obj16/payara-blue.png" ) ));
93- reg .put (EAR_MODULE_IMG , createFromURL ( getBundle (). getEntry ( "icons/obj16/ear.gif" ) ));
94- reg .put (EJB_MODULE_IMG , createFromURL ( getBundle (). getEntry ( "icons/obj16/ejb_module.gif" ) ));
95- reg .put (LOG_FILE_IMG , createFromURL ( getBundle (). getEntry ( "icons/obj16/logfile.png" ) ));
96- reg .put (WEB_MODULE_IMG , createFromURL ( getBundle (). getEntry ( "icons/obj16/web_module.gif" ) ));
97- reg .put (WEBSERVICE_IMG , createFromURL ( getBundle (). getEntry ( "icons/obj16/webservice.png" ) ));
98- reg .put (RESOURCES_IMG , createFromURL ( getBundle (). getEntry ( "icons/obj16/resources.gif" ) ));
99- reg .put (GF_WIZARD , createFromURL ( getBundle (). getEntry ( "icons/wizard75x66.png" ) ));
96+ reg .put (GF_SERVER_IMG , createImageDescriptor ( "icons/obj16/payara-blue.png" ));
97+ reg .put (EAR_MODULE_IMG , createImageDescriptor ( "icons/obj16/ear.gif" ));
98+ reg .put (EJB_MODULE_IMG , createImageDescriptor ( "icons/obj16/ejb_module.gif" ));
99+ reg .put (LOG_FILE_IMG , createImageDescriptor ( "icons/obj16/logfile.png" ));
100+ reg .put (WEB_MODULE_IMG , createImageDescriptor ( "icons/obj16/web_module.gif" ));
101+ reg .put (WEBSERVICE_IMG , createImageDescriptor ( "icons/obj16/webservice.png" ));
102+ reg .put (RESOURCES_IMG , createImageDescriptor ( "icons/obj16/resources.gif" ));
103+ reg .put (GF_WIZARD , createImageDescriptor ( "icons/wizard75x66.png" ));
100104 }
101105
106+ public static ImageDescriptor createImageDescriptor (String path ) {
107+ if (BUNDLE == null ) {
108+ return ImageDescriptor .getMissingImageDescriptor ();
109+ }
102110
103- /**
111+ var imageUrl = BUNDLE .getEntry (path );
112+ if (imageUrl == null ) {
113+ logError ("Unable to locate image resource: " + path );
114+ return ImageDescriptor .getMissingImageDescriptor ();
115+ }
116+
117+ try (InputStream input = imageUrl .openStream ()) {
118+ byte [] imageBytes = input .readAllBytes ();
119+ return ImageDescriptor .createFromImageData (new ImageData (new ByteArrayInputStream (imageBytes )));
120+ } catch (IOException | SWTException | IllegalArgumentException e ) {
121+ logError ("Unable to load image resource: " + path , e );
122+ return ImageDescriptor .getMissingImageDescriptor ();
123+ }
124+ }
125+
126+ /**
104127 * Return the image with the given key from the image registry.
105128 * @param key java.lang.String
106129 * @return org.eclipse.jface.parts.IImage
@@ -118,26 +141,6 @@ public static ImageDescriptor getImageDescriptor(String key) {
118141 return getInstance ().getImageRegistry ().getDescriptor (key );
119142 }
120143
121- // /**
122- // * Register an image with the registry.
123- // * @param key java.lang.String
124- // * @param partialURL java.lang.String
125- // */
126- // private void registerImage(ImageRegistry registry, String key, String partialURL) {
127- // if (ICON_BASE_URL == null) {
128- // String pathSuffix = "icons/";
129- // ICON_BASE_URL = singleton.getBundle().getEntry(pathSuffix);
130- // }
131- //
132- // try {
133- // ImageDescriptor id = ImageDescriptor.createFromURL(new URL(ICON_BASE_URL, partialURL));
134- // registry.put(key, id);
135- // imageDescriptors.put(key, id);
136- // } catch (Exception e) {
137- // Trace.trace(Trace.WARNING, "Error registering image", e);
138- // }
139- // }
140-
141144 @ Override
142145 public void stop (BundleContext v ) throws Exception {
143146 logMessage ("STOP IS CALLED!!!!!!!!!!!!!!!!" );
@@ -216,4 +219,4 @@ public static boolean is31OrAbove(IRuntime runtime) {
216219 return runtime .getRuntimeType ().getId ().equals (RUNTIME_TYPE );
217220 }
218221
219- }
222+ }
0 commit comments