View Javadoc

1   package net.sourceforge.blogentis.utils;
2   
3   //-----------------------------------------------------------------------
4   //Blogentis - a blog publishing platform.
5   //Copyright (C) 2004 Tassos Bassoukos <abassouk@gmail.com>
6   //
7   //This library is free software; you can redistribute it and/or
8   //modify it under the terms of the GNU Lesser General Public
9   //License as published by the Free Software Foundation; either
10  //version 2.1 of the License, or (at your option) any later version.
11  //
12  //This library is distributed in the hope that it will be useful,
13  //but WITHOUT ANY WARRANTY; without even the implied warranty of
14  //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  //Lesser General Public License for more details.
16  //
17  //You should have received a copy of the GNU Lesser General Public
18  //License along with this library; if not, write to the Free Software
19  //Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  //-----------------------------------------------------------------------
21  //
22  //$Id: LinkFactoryService.java,v 1.2 2004/11/02 00:55:22 tassos Exp $
23  //
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  import org.apache.turbine.Turbine;
28  import org.apache.turbine.services.InitializationException;
29  import org.apache.turbine.services.TurbineBaseService;
30  import org.apache.turbine.services.TurbineServices;
31  
32  public class LinkFactoryService
33          extends TurbineBaseService {
34      private static Log log = LogFactory.getLog(LinkFactoryService.class);
35  
36      public static final String SERVICE_NAME = "LinkFactoryService";
37  
38      protected String baseURI = null;
39  
40      public LinkFactoryService() {
41          super();
42          if (log.isDebugEnabled())
43              log.debug("created self");
44      }
45  
46      public static LinkFactoryService getInstance() {
47          return (LinkFactoryService)TurbineServices.getInstance()
48              .getService(SERVICE_NAME);
49      }
50  
51      public static AbsoluteLinkURL getLink() {
52          LinkFactoryService service = getInstance();
53          if (!service.getInit())
54              return null;
55          return service.getLinkImpl();
56      }
57  
58      protected String getBase() {
59          if (baseURI == null) {
60              StringBuffer buffer = new StringBuffer();
61              if (!getConfiguration().getBoolean("omitFullURI",false)) {
62                  buffer.append(Turbine.getServerScheme());
63                  buffer.append("://");
64                  buffer.append(Turbine.getServerName());
65                  if ((Turbine.getServerScheme() == "http" && Integer
66                      .parseInt(Turbine.getServerPort()) != 80)) {
67                      buffer.append(":");
68                      buffer.append(Turbine.getServerPort());
69                  }
70              }
71              buffer.append(Turbine.getContextPath());
72              if (buffer.lastIndexOf("/") < buffer.length() - 1)
73                  buffer.append("/");
74              baseURI = buffer.toString();
75              if (log.isDebugEnabled())
76                  log.debug("our URI is now: " + baseURI);
77          }
78          return baseURI;
79      }
80  
81      public static String getBaseURI() {
82          return getInstance().getBase();
83      }
84  
85      protected AbsoluteLinkURL getLinkImpl() {
86          return new AbsoluteLinkURL();
87      }
88  
89      public void init(Object ignored)
90              throws InitializationException {
91          setInit(true);
92      }
93  }