langtools/src/share/classes/com/sun/tools/javadoc/api/JavadocTaskImpl.java
changeset 14545 2e7bab0639b8
equal deleted inserted replaced
14544:d71d992cb905 14545:2e7bab0639b8
       
     1 /*
       
     2  * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.  Oracle designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Oracle in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    22  * or visit www.oracle.com if you need additional information or have any
       
    23  * questions.
       
    24  */
       
    25 package com.sun.tools.javadoc.api;
       
    26 
       
    27 import com.sun.tools.javac.util.ClientCodeException;
       
    28 import java.util.Locale;
       
    29 import java.util.concurrent.atomic.AtomicBoolean;
       
    30 
       
    31 import javax.tools.DocumentationTool.DocumentationTask;
       
    32 import javax.tools.JavaFileObject;
       
    33 
       
    34 import com.sun.tools.javac.util.Context;
       
    35 import com.sun.tools.javadoc.Start;
       
    36 import java.util.Collections;
       
    37 
       
    38 /**
       
    39  * Provides access to functionality specific to the JDK documentation tool,
       
    40  * javadoc.
       
    41  *
       
    42  * <p><b>This is NOT part of any supported API.
       
    43  * If you write code that depends on this, you do so at your own
       
    44  * risk.  This code and its internal interfaces are subject to change
       
    45  * or deletion without notice.</b></p>
       
    46  */
       
    47 public class JavadocTaskImpl implements DocumentationTask {
       
    48     private final AtomicBoolean used = new AtomicBoolean();
       
    49 
       
    50     private final Context context;
       
    51     private Class<?> docletClass;
       
    52     private Iterable<String> options;
       
    53     private Iterable<? extends JavaFileObject> fileObjects;
       
    54     private Locale locale;
       
    55 
       
    56     public JavadocTaskImpl(Context context, Class<?> docletClass,
       
    57             Iterable<String> options, Iterable<? extends JavaFileObject> fileObjects) {
       
    58         this.context = context;
       
    59         this.docletClass = docletClass;
       
    60 
       
    61         this.options = (options == null) ? Collections.<String>emptySet()
       
    62                 : nullCheck(options);
       
    63         this.fileObjects = (fileObjects == null) ? Collections.<JavaFileObject>emptySet()
       
    64                 : nullCheck(fileObjects);
       
    65         setLocale(Locale.getDefault());
       
    66     }
       
    67 
       
    68     public void setLocale(Locale locale) {
       
    69         if (used.get())
       
    70             throw new IllegalStateException();
       
    71         this.locale = locale;
       
    72     }
       
    73 
       
    74     public Boolean call() {
       
    75         if (!used.getAndSet(true)) {
       
    76             initContext();
       
    77             Start jdoc = new Start(context);
       
    78             try {
       
    79                 return jdoc.begin(docletClass, options, fileObjects);
       
    80             } catch (ClientCodeException e) {
       
    81                 throw new RuntimeException(e.getCause());
       
    82             }
       
    83         } else {
       
    84             throw new IllegalStateException("multiple calls to method 'call'");
       
    85         }
       
    86     }
       
    87 
       
    88     private void initContext() {
       
    89         //initialize compiler's default locale
       
    90         context.put(Locale.class, locale);
       
    91     }
       
    92 
       
    93     private static <T> Iterable<T> nullCheck(Iterable<T> items) {
       
    94         for (T item: items) {
       
    95             if (item == null)
       
    96                 throw new NullPointerException();
       
    97         }
       
    98         return items;
       
    99     }
       
   100 }