001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.project.libraries;
043:
044: import java.io.IOException;
045: import java.net.URL;
046: import java.util.List;
047: import java.util.Map;
048: import java.util.Set;
049: import org.netbeans.api.project.libraries.Library;
050: import org.netbeans.spi.project.libraries.ArealLibraryProvider;
051: import org.netbeans.spi.project.libraries.LibraryImplementation;
052: import org.netbeans.spi.project.libraries.LibraryProvider;
053: import org.netbeans.spi.project.libraries.LibraryStorageArea;
054: import org.openide.util.Exceptions;
055:
056: public abstract class LibraryAccessor {
057:
058: public static LibraryAccessor DEFAULT;
059:
060: // force loading of Library class. That will set DEFAULT variable.
061: static {
062: try {
063: Object o = Class.forName(
064: "org.netbeans.api.project.libraries.Library", true,
065: LibraryAccessor.class.getClassLoader());
066: } catch (ClassNotFoundException cnf) {
067: Exceptions.printStackTrace(cnf);
068: }
069: }
070:
071: public abstract Library createLibrary(
072: LibraryImplementation libraryImplementation);
073:
074: // RADIKAL GENERIC HAX!
075:
076: /**
077: * Type-safe accessor for {@link ArealLibraryProvider#remove}.
078: * @throws ClassCastException if the runtime types do not match
079: */
080: public static void remove(ArealLibraryProvider alp,
081: LibraryImplementation lib) throws IOException {
082: remove0((ArealLibraryProvider<?, ?>) alp, lib);
083: }
084:
085: private static <L extends LibraryImplementation> void remove0(
086: ArealLibraryProvider<?, L> alp, LibraryImplementation lib)
087: throws IOException {
088: alp.remove(alp.libraryType().cast(lib));
089: }
090:
091: /**
092: * Type-safe accessor for {@link ArealLibraryProvider#getOpenAreas}.
093: */
094: public static Set<? extends LibraryStorageArea> getOpenAreas(
095: ArealLibraryProvider alp) {
096: return ((ArealLibraryProvider<?, ?>) alp).getOpenAreas();
097: }
098:
099: /**
100: * Type-safe accessor for {@link ArealLibraryProvider#createLibrary}.
101: * @throws ClassCastException if the runtime types do not match
102: */
103: public static LibraryImplementation createLibrary(
104: ArealLibraryProvider alp, String type, String name,
105: LibraryStorageArea area, Map<String, List<URL>> contents)
106: throws IOException {
107: return createLibrary0(((ArealLibraryProvider<?, ?>) alp), type,
108: name, area, contents);
109: }
110:
111: private static <A extends LibraryStorageArea> LibraryImplementation createLibrary0(
112: ArealLibraryProvider<A, ?> alp, String type, String name,
113: LibraryStorageArea area, Map<String, List<URL>> contents)
114: throws IOException {
115: return alp.createLibrary(type, name, alp.areaType().cast(area),
116: contents);
117: }
118:
119: /**
120: * Type-safe accessor for {@link ArealLibraryProvider#getLibraries}.
121: * @throws ClassCastException if the runtime types do not match
122: */
123: public static LibraryProvider getLibraries(
124: ArealLibraryProvider alp, LibraryStorageArea area) {
125: return getLibraries0((ArealLibraryProvider<?, ?>) alp, area);
126: }
127:
128: private static <A extends LibraryStorageArea> LibraryProvider getLibraries0(
129: ArealLibraryProvider<A, ?> alp, LibraryStorageArea area) {
130: return alp.getLibraries(alp.areaType().cast(area));
131: }
132:
133: }
|