1+ /**
2+ * Copyright (C) 2016-2024 Ignite Realtime Foundation. All rights reserved.
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+ */
116package org .igniterealtime .openfire .plugin ;
217
3- import org .dom4j .Document ;
4- import org .dom4j .DocumentException ;
5- import org .dom4j .Element ;
6- import org .dom4j .io .SAXReader ;
718import org .jivesoftware .openfire .container .Plugin ;
819import org .jivesoftware .openfire .container .PluginManager ;
20+ import org .jivesoftware .openfire .container .PluginMetadataHelper ;
921import org .jivesoftware .openfire .plugin .spark .BookmarkInterceptor ;
10- import org .jivesoftware .util .JiveGlobals ;
1122import org .jivesoftware .util .Version ;
1223import org .slf4j .Logger ;
1324import org .slf4j .LoggerFactory ;
1425
26+ import javax .annotation .Nonnull ;
1527import java .io .*;
16- import java .util .jar .JarFile ;
17- import java .util .zip .ZipEntry ;
1828
1929/**
2030 * A plugin that implements XEP-0048 "Bookmarks".
@@ -34,14 +44,14 @@ public void initializePlugin( PluginManager manager, File pluginDirectory )
3444 try
3545 {
3646 // Check if we Enterprise is installed and stop loading this plugin if found
37- if ( checkForEnterprisePlugin () )
47+ if ( checkForEnterprisePlugin (manager ) )
3848 {
3949 System .out .println ( "Enterprise plugin found. Stopping Bookmarks Plugin." );
4050 foundIncompatiblePlugin = true ;
4151 }
4252
4353 // Check if we ClientControl (version <= 1.3.1) is installed and stop loading this plugin if found
44- if ( checkForIncompatibleClientControlPlugin () )
54+ if ( checkForIncompatibleClientControlPlugin (manager ) )
4555 {
4656 System .out .println ( "ClientControl plugin v1.3.1 or earlier found. Stopping Bookmarks Plugin." );
4757 foundIncompatiblePlugin = true ;
@@ -75,83 +85,29 @@ public void destroyPlugin()
7585 /**
7686 * Checks if there's a plugin named "enterprise" in the Openfire plugin directory.
7787 *
88+ * @param manager The Openfire plugin manager
7889 * @return true if the enterprise plugin is found, otherwise false.
7990 */
80- private static boolean checkForEnterprisePlugin () throws IOException
91+ private static boolean checkForEnterprisePlugin (@ Nonnull final PluginManager manager )
8192 {
82- return getPluginJar ( "enterprise" ) != null ;
93+ return manager . getPluginByName ( "enterprise" ). isPresent () ;
8394 }
8495
8596 /**
8697 * Checks if there's a plugin named "clientControl" in the Openfire plugin directory of which the version is equal
8798 * to or earlier than 1.3.1.
8899 *
100+ * @param manager The Openfire plugin manager
89101 * @return true if the clientControl plugin (<= 1.3.1) is found, otherwise false.
90102 */
91- private static boolean checkForIncompatibleClientControlPlugin () throws IOException , DocumentException
103+ private static boolean checkForIncompatibleClientControlPlugin (@ Nonnull final PluginManager manager )
92104 {
93- final JarFile jar = getPluginJar ( "clientControl" );
94-
95- if ( jar == null )
96- {
97- return false ;
98- }
99-
100- final ZipEntry pluginXml = jar .getEntry ( "plugin.xml" );
101- if ( pluginXml == null )
102- {
103- // Odd - not a plugin?
104- Log .warn ( "Found a clientControl.jar file that does not appear to include a plugin.xml." , jar .getName () );
105+ final Plugin clientControlPlugin = manager .getPluginByName ("clientControl" ).orElse (null );
106+ if (clientControlPlugin == null ) {
105107 return false ;
106108 }
107109
108- final File tempFile = File .createTempFile ( "plugin-xml" , "xml" );
109- try ( final InputStream is = jar .getInputStream ( pluginXml );
110- final FileOutputStream os = new FileOutputStream ( tempFile ) )
111- {
112- while ( is .available () > 0 )
113- {
114- os .write ( is .read () );
115- }
116-
117- final SAXReader saxReader = new SAXReader ();
118- saxReader .setEncoding ( "UTF-8" );
119- final Document pluginXML = saxReader .read ( tempFile );
120- Element element = (Element ) pluginXML .selectSingleNode ( "/plugin/version" );
121- if ( element != null )
122- {
123- final Version version = new Version ( element .getTextTrim () );
124- return !version .isNewerThan ( new Version ( "1.3.1" ) );
125- }
126- }
127- return false ;
128- }
129-
130- /**
131- * Returns the plugin JAR for the plugin of the provided name.
132- *
133- * @param pluginName the name of the plugin (cannot be null or empty).
134- * @return The plugin JAR file, or null when not found.
135- */
136- private static JarFile getPluginJar ( final String pluginName ) throws IOException
137- {
138- File pluginDir = new File ( JiveGlobals .getHomeDirectory (), "plugins" );
139- File [] jars = pluginDir .listFiles ( new FileFilter ()
140- {
141- public boolean accept ( File pathname )
142- {
143- return pathname .getName ().equalsIgnoreCase ( pluginName + ".jar" );
144- }
145- } );
146-
147- final File jar ;
148- if ( jars .length > 0 )
149- {
150- return new JarFile ( jars [ 0 ] );
151- }
152- else
153- {
154- return null ;
155- }
110+ final Version version = PluginMetadataHelper .getVersion (clientControlPlugin );
111+ return !version .isNewerThan ( new Version ( "1.3.1" ) );
156112 }
157113}
0 commit comments