summaryrefslogtreecommitdiff
path: root/src/plugins/db.core/src/com/motorolamobility/studio/android/db/core/DbCoreActivator.java
blob: 6251372cc62e791641bafd58a1a0b9f879456702 (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
/*
 * 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.motorolamobility.studio.android.db.core;

import static com.motorola.studio.android.common.log.StudioLogger.debug;
import static com.motorola.studio.android.common.log.StudioLogger.error;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtension;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;
import org.eclipse.osgi.util.NLS;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;

import com.motorola.studio.android.common.CommonPlugin;
import com.motorola.studio.android.common.log.StudioLogger;
import com.motorola.studio.android.common.utilities.EclipseUtils;
import com.motorola.studio.android.common.utilities.FileUtil;
import com.motorolamobility.studio.android.db.core.model.DbModel;
import com.motorolamobility.studio.android.db.core.ui.view.MOTODEVDatabaseExplorerView;

/**
 * The activator class controls the plug-in life cycle
 */
public class DbCoreActivator extends AbstractUIPlugin
{

    private static final String DB_PROPERTY_TESTER_ATT_PROPERTIES = "properties";

    private static final String DB_PROPERTY_TESTER_ATT_NAMESPACE = "namespace";

    private static final String DB_PROPERTY_TESTER_EXTENSION_ID =
            "com.motorolamobility.studio.android.db.core.propertyTesters";

    // The plug-in ID
    public static final String PLUGIN_ID = "com.motorolamobility.studio.android.db.core"; //$NON-NLS-1$

    private static final String DB_TEMPLATE_PATH = "res/template.db"; //$NON-NLS-1$

    private static List<String> pluginProperties = null;

    // The shared instance
    private static DbCoreActivator plugin;

    public static final String DATATOOLS_UI_PLUGIN_ID =
    "org.eclipse.datatools.connectivity.sqm.core.ui"; //$NON-NLS-1$

    public static final String TABLE_ICON = "icons/table.gif"; //$NON-NLS-1$

    /*
     * (non-Javadoc)
     * @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext)
     */
    @Override
    public void start(BundleContext context) throws Exception
    {
        StudioLogger.debug(DbCoreActivator.class,
                "Starting MOTODEV Studio for Android Database Core Plugin...");

        super.start(context);
        plugin = this;

        DbModel.assertDriverExistsAtModel();
        DbModel.cleanPreviousProfiles();

        StudioLogger.debug(DbCoreActivator.class,
                "MOTODEV Studio for Android Database Core Plugin started.");
    }

    /*
     * (non-Javadoc)
     * @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext)
     */
    @Override
    public void stop(BundleContext context) throws Exception
    {
        plugin = null;
        DbModel.deleteDriverFromModel();
        super.stop(context);
    }

    /**
     * Returns the shared instance
     *
     * @return the shared instance
     */
    public static DbCoreActivator getDefault()
    {
        return plugin;
    }

    /*
     * Returns a File object representing a path inside this plug-in
     */
    private File getResourceFile(String pathAtPlugin)
    {
        URL location = this.getBundle().getEntry(pathAtPlugin);

        File file = null;
        try
        {
            IPath p = new Path(FileLocator.toFileURL(location).getFile());
            file = p.toFile();
        }
        catch (IOException e)
        {
            error(NLS.bind("Could not get resource file {0}", pathAtPlugin)); //$NON-NLS-1$
        }

        return file;

    }

    /**
     * Retrieves the location of the SQLITE3 jdbc driver
     * @return
     */
    public String getDriverPath()
    {
        return CommonPlugin.getDefault().getDriverPath();
    }

    /*
     * Retrieves the templateDbFile
     */
    private File getTemplateDbFile()
    {
        return getResourceFile(DB_TEMPLATE_PATH);
    }

    /**
     * Copy the template db file to a given target file
     * @param target
     * @throws IOException If file path already exists or if the copy operation failed for some reason.
     */
    public void copyTemplateDbFile(File target, boolean overwrite) throws IOException
    {
        debug(NLS.bind("Attempting to copy db template file to {0}", target.getAbsolutePath())); //$NON-NLS-1$
        File templateDbFile = getTemplateDbFile();
        if (overwrite && target.exists())
        {
            target.delete();
        }

        if (overwrite || !target.exists())
        {
            debug(NLS.bind("Creating parent folders for file: {0}", target.getAbsolutePath())); //$NON-NLS-1$
            target.getParentFile().mkdirs(); //Create parent folder if needed.
            FileUtil.copyFile(templateDbFile, target);
        }
        else
        {
            throw new IOException("Target file already exists"); //$NON-NLS-1$
        }
        debug(NLS.bind("DB template file succesfully copyed to {0}", target.getAbsolutePath())); //$NON-NLS-1$
    }

    /**
     * Retrieves the {@link MOTODEVDatabaseExplorerView} instance if it's active.
     * @return the active instance of the {@link MOTODEVDatabaseExplorerView}, or null if there's no active db view.
     */
    public static MOTODEVDatabaseExplorerView getMOTODEVDatabaseExplorerView()
    {
        IViewPart view = EclipseUtils.getActiveView(MOTODEVDatabaseExplorerView.VIEW_ID);

        if (view instanceof MOTODEVDatabaseExplorerView)
        {
            return (MOTODEVDatabaseExplorerView) view;
        }

        return null;
    }

    /**
     * @return all namespaced properties defined by org.eclipse.core.expressions.propertyTesters extensions in this plugin.xml file.
     * 
     * */
    public static List<String> getPluginProperties()
    {
        //only load properties from extension registry once
        if (pluginProperties == null)
        {
            pluginProperties = new ArrayList<String>();

            IExtension propertyTesters =
                    Platform.getExtensionRegistry().getExtension(DB_PROPERTY_TESTER_EXTENSION_ID);

            IConfigurationElement[] confElements = propertyTesters.getConfigurationElements();

            for (IConfigurationElement confElement : confElements)
            {
                String namespace = confElement.getAttribute(DB_PROPERTY_TESTER_ATT_NAMESPACE);
                String property = confElement.getAttribute(DB_PROPERTY_TESTER_ATT_PROPERTIES);

                String[] properties = property.split(",");
                for (String prop : properties)
                {
                    String namespacedProperty = namespace.concat(".").concat(prop);
                    pluginProperties.add(namespacedProperty);
                }
            }
        }

        return pluginProperties;
    }
}