summaryrefslogtreecommitdiff
path: root/src/plugins/common/src/com/motorola/studio/android/common/utilities/PluginUtils.java
blob: 2389f43df989ccd67de9f6750bdb889bfc332b63 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
/*
* Copyright (C) 2012 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*      http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.motorola.studio.android.common.utilities;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map;

import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtension;
import org.eclipse.core.runtime.IExtensionPoint;
import org.eclipse.core.runtime.IExtensionRegistry;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Plugin;
import org.eclipse.osgi.util.NLS;
import org.osgi.framework.Bundle;

import com.motorola.studio.android.common.exception.AndroidException;
import com.motorola.studio.android.common.log.StudioLogger;
import com.motorola.studio.android.common.utilities.i18n.UtilitiesNLS;

/**
 * DESCRIPTION: This class serves as an utility class containing only static methods such as getters
 * for plugin attributes, plugin resources, installation path etc
 *
 * USAGE: Import this class then use the static methods.
 */
public class PluginUtils
{
    public static final int OS_WINDOWS = 0;

    public static final int OS_LINUX = 1;

    /**
     * Returns a plugin attribute using the extension as parameter.
     *
     * @param fromExtension
     *         the extension from which the attribute should be collected
     * @param element
     *         the extension element
     * @param attribute
     *         the extension attribute
     *
     * @return
     *         the value of the extension attribute
     *
     * @throws MotodevExtensionException if the executable cannot be created for any reason
     */
    public static Object getExecutable(String extensionId, String elementName, String executableName)
            throws Exception

    {
        Object executable = null;

        IExtension fromExtension = getExtension(extensionId);

        if ((fromExtension != null) && (elementName != null))
        {
            IConfigurationElement[] elements = fromExtension.getConfigurationElements();

            for (IConfigurationElement element : elements)
            {
                if (elementName.equals(element.getName()))
                {
                    try
                    {
                        executable = element.createExecutableExtension(executableName);
                    }
                    catch (Exception e)
                    {
                        String errMsg =
                                NLS.bind(
                                        UtilitiesNLS.EXC_PluginUtils_ErrorGettingTheExecutableFromExtensionPoint,
                                        new Object[]
                                        {
                                                executableName, elementName, extensionId
                                        });
                        StudioLogger.error(PluginUtils.class, errMsg, e);

                        throw new Exception(errMsg, e);
                    }
                }
            }
        }

        return executable;
    }

    /**
     * Returns a plugin attribute using the extension as parameter.
     *
     * @param fromExtension
     *         the extension from which the attribute should be collected
     * @param element
     *         the extension element
     * @param attribute
     *         the extension attribute
     *
     * @return
     *         the value of the extension attribute
     *
     * @throws MotodevExtensionException if the executable cannot be created for any reason
     */
    public static Object getExecutable(String extensionId, String elementName) throws Exception
    {
        return getExecutable(extensionId, elementName, "class");
    }

    /**
     * Returns the extension using as parameters the id of the extension
     * and the id of its extension point.
     *
     * @param extensionPointId
     *         the id of the extension point
     * @param extensionId
     *         the id of the extension
     *
     * @return
     *         the extension
     */
    public static IExtension getExtension(String extensionPointId, String extensionId)
    {
        IExtensionRegistry registry = Platform.getExtensionRegistry();
        IExtension extension = registry.getExtension(extensionPointId, extensionId);

        return extension;
    }

    /**
     * Returns the extension using as parameter only the id of the extension.
     *
     * @param extensionId
     *         the id of the extension
     *
     * @return
     *         the extension
     */
    public static IExtension getExtension(String extensionId)
    {
        IExtensionRegistry registry = Platform.getExtensionRegistry();
        IExtension extension = registry.getExtension(extensionId);

        return extension;
    }

    /**
     * Returns the label for the extension (extension name) using as parameters
     * the id of the extension and the id of its extension point.
     *
     * @param extensionPointId
     *         the id of the extension point
     * @param extensionId
     *         the id of the extension
     *
     * @return
     *         the extension label
     */
    public static String getExtensionLabel(String extensionPointId, String extensionId)
    {
        IExtension extension = getExtension(extensionPointId, extensionId);
        String extensionLabel;

        if (extension != null)
        {
            extensionLabel = extension.getLabel();
        }
        else
        {
            extensionLabel = extensionId;
        }

        return extensionLabel;
    }

    /**
     * Returns the label for the extension (extension name) using as parameter only
     * the id of the extension.
     *
     * @param extensionId
     *         the id of the extension
     *
     * @return
     *         the extension label
     */
    public static String getExtensionLabel(String extensionId)
    {
        IExtension extension = getExtension(extensionId);
        String extensionLabel;

        if (extension != null)
        {
            extensionLabel = extension.getLabel();
        }
        else
        {
            extensionLabel = extensionId;
        }

        return extensionLabel;
    }

    /**
     * Returns a collection of strings containing the ids of installed plugins.
     *
     * @param extensionPointId
     *         the id of the extension point
     *
     * @return
     *         a collection object containing the ids of the installed plugins
     */
    public static Collection<String> getInstalledPlugins(String extensionPointId)
    {
        IExtensionRegistry registry = Platform.getExtensionRegistry();
        IExtensionPoint extensionPoint = registry.getExtensionPoint(extensionPointId);
        Collection<String> pluginIds = new LinkedHashSet<String>();

        if (extensionPoint != null)
        {
            for (IExtension extension : extensionPoint.getExtensions())
            {
                pluginIds.add(extension.getUniqueIdentifier());
            }
        }

        return pluginIds;
    }

    /**
     * Fills an array object with the ids contained in the collection object returned by
     * {@link #getInstalledPlugins(String)}.
     *
     * @param extensionPointId
     *         the id of the extension point
     *
     * @return
     *         an array object containing the ids of the installed plugins
     */
    public static String[] getInstalledPluginsAsArray(String extensionPointId)
    {
        Collection<String> sampleAppPluginIds = getInstalledPlugins(extensionPointId);
        String[] sampleAppPluginIdsArray = new String[sampleAppPluginIds.size()];

        return sampleAppPluginIds.toArray(sampleAppPluginIdsArray);
    }

    /**
     * Retrieves the namespaces used by the platform.
     *
     *
     * @return a collection with the namespaces used by the platform
     */
    public static Collection<String> getPlatformNamespaces()
    {
        IExtensionRegistry extensionRegistry = Platform.getExtensionRegistry();
        String[] namespaces = extensionRegistry.getNamespaces();

        return Arrays.asList(namespaces);
    }

    /**
     * Returns a plugin attribute using the extension id and the extension point id as parameters.
     *
     * @param extensionPointId
     *         the id of the extension point of the exten sion
     * @param extensionId
     *         the id of the extension
     * @param element
     *         the extension element
     * @param attribute
     *         the extension attribute
     *
     * @return
     *         the value of the extension attribute
     */
    public static String getPluginAttribute(String extensionPointId, String extensionId,
            String element, String attribute)
    {
        IExtension fromPlugin = getExtension(extensionPointId, extensionId);

        return getPluginAttribute(fromPlugin, element, attribute);
    }

    /**
     * Returns a plugin attribute using the extension id and the extension point id as parameters.
     *
     * @param extensionId
     *         the id of the extension
     * @param element
     *         the extension element
     * @param attribute
     *         the extension attribute
     *
     * @return
     *         the value of the extension attribute
     */
    public static String getPluginAttribute(String extensionId, String element, String attribute)
    {
        IExtension fromPlugin = getExtension(extensionId);

        return getPluginAttribute(fromPlugin, element, attribute);
    }

    /**
     * Returns a plugin attribute using the extension as parameter.
     *
     * @param fromExtension
     *         the extension from which the attribute should be collected
     * @param element
     *         the extension element
     * @param attribute
     *         the extension attribute
     *
     * @return
     *         the value of the extension attribute
     */
    public static String getPluginAttribute(IExtension fromExtension, String element,
            String attribute)
    {
        String attributeValue = null;

        if (fromExtension != null)
        {
            IConfigurationElement[] ceArray = fromExtension.getConfigurationElements();

            for (IConfigurationElement ce : ceArray)
            {
                if ((ce != null) && ce.getName().equals(element))
                {
                    attributeValue = ce.getAttribute(attribute);
                }
            }
        }

        return attributeValue;
    }

    /**
     * DOCUMENT ME
     *
     * @param extensionId DOCUMENT ME!
     * @param element DOCUMENT ME!
     *
     * @return DOCUMENT ME!
     */
    public static Collection<Map<String, String>> getPluginAttributes(String extensionId,
            String element)
    {
        IExtension fromExtension = getExtension(extensionId);

        return getPluginAttributes(fromExtension, element);
    }

    /**
     * DOCUMENT ME
     *
     * @param fromExtension DOCUMENT ME!
     * @param element DOCUMENT ME!
     *
     * @return DOCUMENT ME!
     */
    public static Collection<Map<String, String>> getPluginAttributes(IExtension fromExtension,
            String element)
    {
        Collection<Map<String, String>> elementValues = new LinkedHashSet<Map<String, String>>();

        if (fromExtension != null)
        {
            IConfigurationElement[] ceArray = fromExtension.getConfigurationElements();

            for (IConfigurationElement ce : ceArray)
            {
                if ((ce != null) && ce.getName().equals(element))
                {
                    String[] attributes = ce.getAttributeNames();

                    if ((attributes != null) && (attributes.length > 0))
                    {
                        int attributesLenght = attributes.length;
                        Map<String, String> attributesMap =
                                new LinkedHashMap<String, String>(attributesLenght,
                                        attributesLenght);

                        for (String attribute : attributes)
                        {
                            String attributeValue = ce.getAttribute(attribute);

                            if ((attributeValue != null) && (!attributeValue.equals("")))
                            {
                                attributesMap.put(attribute, attributeValue);
                            }
                        }

                        // only add to elementValues if all values were read correctly
                        if (attributesMap.size() == attributesLenght)
                        {
                            elementValues.add(attributesMap);
                        }
                    }
                }
            }
        }

        return elementValues;
    }

    /**
     * Returns the absolute path of installation as a file object using the plugin as parameter.
     *
     * @param plugin
     *         the plugin installed
     *
     * @return
     *         a file object pointing to the installation path of the plugin
     */
    public static File getPluginInstallationPath(Plugin plugin)
    {
        Bundle pluginBundle = plugin.getBundle();

        return getPluginInstallationPath(pluginBundle);
    }

    /**
     * Returns the absolute path of installation as a file object using the ids of the extension
     * and extension point as parameters.
     *
     * @param extensionPointId
     *         the id of the extension point
     * @param extensionId
     *         the id of the extension
     *
     * @return
     *         a file object pointing to the installation path of the plugin
     */
    public static File getPluginInstallationPath(String extensionPointId, String extensionId)
    {
        IExtension extension = getExtension(extensionPointId, extensionId);

        return getPluginInstallationPath(extension);
    }

    /**
     * Returns the absolute path of installation as a file object using the extension as parameter.
     *
     * @param extension
     *         the extension object
     *
     * @return
     *         a file object pointing to the installation path of the plugin
     */
    public static File getPluginInstallationPath(IExtension extension)
    {
        String pluginId = extension.getNamespaceIdentifier();
        Bundle pluginBundle = Platform.getBundle(pluginId);

        return getPluginInstallationPath(pluginBundle);
    }

    /**
     * Returns the absolute path of installation as a file object using the plugin bundle as parameter.
     *
     * @param pluginBundle
     *         the plugin bundle
     *
     * @return
     *         a file object pointing to the installation path of the plugin
     */
    public static File getPluginInstallationPath(Bundle pluginBundle)
    {
        //get file using FileLocator
        File relativeInstalationPath = null;
        try
        {
            relativeInstalationPath =
                    new File(FileLocator.toFileURL(pluginBundle.getEntry("")).getFile());
        }
        catch (IOException e)
        {
            StudioLogger.warn("Illegal state while getting plugin installation path ("
                    + e.getMessage() + ").");
        }

        //if failed to get the file using FileLocator        
        if (relativeInstalationPath == null)
        {
            String platformPath = Platform.getInstallLocation().getURL().getPath();
            String pluginPath = pluginBundle.getLocation();
            int removeIndex = pluginPath.indexOf("@");
            pluginPath = pluginPath.substring(removeIndex + 1);

            relativeInstalationPath = new File(platformPath, pluginPath);
        }

        return FileUtil.getCanonicalFile(relativeInstalationPath);
    }

    /**
     * Returns a file object from the path: $installationPath\resource
     *
     * @param plugin
     *         the plugin object
     *
     * @param resource
     *         the plugin resource
     *
     * @return
     *         a file object pointing to the path of the resource
     *
     * @throws MotodevResourceNotAvailable
     *         throws an exception if it occurs an I/O exception with the path $installationPath\resource
     */
    public static File getPluginResource(Plugin plugin, String resource) throws Exception
    {
        File pluginPath = getPluginInstallationPath(plugin);
        File resourceFile = new File(pluginPath, resource);
        File canonicalFile = null;

        canonicalFile = FileUtil.getCanonicalFile(resourceFile);

        return canonicalFile;
    }

    /**
     * Checks if an extension is installed using the extension point id and extension id as parameters.
     *
     * @param extensionPointId
     *         the id of the extension point
     * @param extensionId
     *         the id of the extension
     *
     * @return
     *         true if the extension is installed or false otherwise
     */
    public static boolean isInstalled(String extensionPointId, String extensionId)
    {
        return getExtension(extensionPointId, extensionId) != null;
    }

    /**
     * Checks if an extension is installed using the extension id as parameter.
     *
     * @param extensionId
     *         the id of the extension
     *
     * @return
     *         true if the extension is installed or false otherwise
     */
    public static boolean isInstalled(String extensionId)
    {
        return getExtension(extensionId) != null;
    }

    /**
     * Returns which is the OS.
     * @return
     *      a code corresponding to the proper OS
     */
    public static int getOS()
    {
        int result = -1;

        String osName = System.getProperty("os.name").toLowerCase();

        if (osName.indexOf("linux") > -1)
        {
            result = OS_LINUX;
        }

        else if (osName.indexOf("windows") > -1)
        {
            result = OS_WINDOWS;
        }

        return result;
    }

    /**
     * Retrieves the File object representing a file stored into the
     * preferences area of the given plugin
     *
     * @return the File inside the given plugin preferences area.
     * @throws MotodevException if it fails to determine the preferences directory of the given plugin
     */
    public final static File getFileOnPreferenceDirectory(Plugin plugin, String filename)
            throws Exception
    {
        File targetXmlFile = null;

        try
        {
            IPath path = plugin.getStateLocation();

            if (path != null)
            {
                path = path.append(filename);
                targetXmlFile = path.toFile().getAbsoluteFile();
            }
        }
        catch (IllegalStateException e)
        {
            StudioLogger.warn("Illegal state while getting file on preferences directory ("
                    + e.getMessage() + ").");
        }

        if (targetXmlFile == null)
        {
            throw new AndroidException("Could use file " + filename + " on preferences plug-in "
                    + plugin.getBundle().getBundleId());
        }

        return targetXmlFile;
    }
}