/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 org.apache.geronimo.gshell.layout;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.annotations.Annotations;
import com.thoughtworks.xstream.io.xml.DomDriver;
import org.apache.geronimo.gshell.layout.model.Alias;
import org.apache.geronimo.gshell.layout.model.Command;
import org.apache.geronimo.gshell.layout.model.Layout;
import org.codehaus.plexus.PlexusContainer;
import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * The default implementation of a {@link LayoutManager}.
 *
 * @version $Rev$ $Date$
 */
// @Component(role = LayoutManager.class)
public class LayoutManagerImpl
    implements LayoutManager, Initializable
{
    private final Logger log = LoggerFactory.getLogger(getClass());

    // @Requirement
    private PlexusContainer container;

    private Layout layout;
    
    public void initialize() throws InitializationException {
        //
        // HACK: For now just hard code it... Really need to expose a helper object to find the Shell's basic directory config muck
        //

        String homePath = System.getProperty("gshell.home");
        if (homePath == null) {
            throw new InitializationException("The 'gsell.home' property must be set for the shell to function correctly");
        }

        File homeDir = new File(homePath);
        URL url = null;
        try {
            url = new File(homeDir, "etc/layout.xml").toURI().toURL();
        } catch (MalformedURLException e) {
            throw new InitializationException("Invalid URL for layout configuration", e);
        }

        log.debug("Loading layout from XML: {}", url);

        InputStream input;
        try {
            input = url.openStream();
        }
        catch (IOException e) {
            throw new InitializationException("Failed to initialize the shell layout", e);
        }

        // Setup the XStream marshallar and configure it with the aliases for the model we are working with
        XStream xs = new XStream(new DomDriver());
        Annotations.configureAliases(xs, Layout.class, Command.class, Alias.class);

        Layout layout;
        try {
            layout = (Layout)xs.fromXML(input);
            
            assert layout != null;
        }
        finally {
            try {
                input.close();
            }
            catch (IOException ignore) {}
        }

        log.debug("Loaded layout: {}", layout);

        //
        // TODO: Do some kind post-parsing validation or someting?

        this.layout = layout;
    }

    public Layout getLayout() {
        return layout;
    }
}
@@ -2690,276 +2690,355 @@
     
-log.debug(%22Loading layout from XML: %7B%7D%22, url);%0A%0A        InputStream input;%0A        try %7B%0A            input = url.openStream();%0A        %7D%0A        catch (IOException e) %7B%0A            throw new InitializationException(%22Failed to initialize the shell layout%22, e
+try %7B%0A            this.layout = load(url);%0A        %7D%0A        catch (IOException e) %7B%0A            throw new InitializationException(%22Failed to load layout from URL: %22 + url, e);%0A        %7D%0A    %7D%0A%0A    private Layout load(final URL url) throws IOException %7B%0A        assert url != null;%0A%0A        log.debug(%22Loading layout from XML: %7B%7D%22, url
 );%0A
+%0A
         
 %7D%0A%0A 
@@ -3025,33 +3025,69 @@
  url);%0A%0A        
-%7D
+InputStream input = url.openStream();
 %0A%0A        // Set
@@ -3492,101 +3492,22 @@
     
- try %7B%0A                input.close();%0A            %7D%0A            catch (IOException ignore) %7B%7D
+input.close();
 %0A   
@@ -3650,29 +3650,22 @@
         
-this.layout =
+return
  layout;

Move the bits which actually do the loading to load()