001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.gwt.client.rpc;
029
030import org.opencms.gwt.client.CmsCoreProvider;
031import org.opencms.gwt.shared.rpc.I_CmsLogService;
032import org.opencms.gwt.shared.rpc.I_CmsLogServiceAsync;
033
034import com.google.gwt.core.client.GWT;
035import com.google.gwt.core.client.Scheduler;
036import com.google.gwt.core.client.Scheduler.ScheduledCommand;
037import com.google.gwt.user.client.rpc.AsyncCallback;
038import com.google.gwt.user.client.rpc.ServiceDefTarget;
039
040/**
041 * Handles client side logging.<p>
042 *
043 * @since 8.0.0
044 *
045 * @see org.opencms.gwt.CmsLogService
046 * @see org.opencms.gwt.shared.rpc.I_CmsLogService
047 * @see org.opencms.gwt.shared.rpc.I_CmsLogServiceAsync
048 */
049public final class CmsLog {
050
051    /** The service instance. */
052    private static I_CmsLogServiceAsync m_loggingService;
053
054    /**
055     * Prevent instantiation.<p>
056     */
057    private CmsLog() {
058
059        // Prevent instantiation
060    }
061
062    /**
063     * Logs client messages on the server.<p>
064     *
065     * @param message the message to log
066     *
067     * @return the generated ticket
068     */
069    public static String log(final String message) {
070
071        final String ticket = String.valueOf(System.currentTimeMillis());
072        // using a deferred command just to be more responsible
073        // since we do not expect any feed back from it
074        Scheduler.get().scheduleDeferred(new ScheduledCommand() {
075
076            /**
077             * @see com.google.gwt.user.client.Command#execute()
078             */
079            public void execute() {
080
081                getLoggingService().log(ticket, message, new AsyncCallback<Void>() {
082
083                    /**
084                     * @see com.google.gwt.user.client.rpc.AsyncCallback#onFailure(java.lang.Throwable)
085                     */
086                    public void onFailure(Throwable caught) {
087
088                        // logging failed, really bad
089                        // but the only thing we can do is to ignore it
090                    }
091
092                    /**
093                     * @see com.google.gwt.user.client.rpc.AsyncCallback#onSuccess(Object)
094                     */
095                    public void onSuccess(Void result) {
096
097                        // logged successfully
098                    }
099                });
100            }
101        });
102        return ticket;
103    }
104
105    /**
106     * Returns the service instance, using lazy initialization.<p>
107     *
108     * @return the service instance
109     */
110    protected static I_CmsLogServiceAsync getLoggingService() {
111
112        if (m_loggingService == null) {
113            m_loggingService = GWT.create(I_CmsLogService.class);
114            String serviceUrl = CmsCoreProvider.get().link("org.opencms.gwt.CmsLogService.gwt");
115            ((ServiceDefTarget)m_loggingService).setServiceEntryPoint(serviceUrl);
116
117        }
118        return m_loggingService;
119    }
120}